Skip to content

[API Proposal]: HttpClient.PatchAsJsonAsync() #60531

@ikesnowy

Description

@ikesnowy

Background and motivation

#45405

It's natural to have PatchAsJsonAsync when we already got PostAsJsonAsync and PutAsJsonAsync.

API Proposal

namespace System.Net.Http.Json { public static class HttpClientJsonExtensions { public static Task<HttpResponseMessage> PatchAsJsonAsync<TValue>(this HttpClient client, string? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default); public static Task<HttpResponseMessage> PatchAsJsonAsync<TValue>(this HttpClient client, Uri? requestUri, TValue value, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default); public static Task<HttpResponseMessage> PatchAsJsonAsync<TValue>(this HttpClient client, string? requestUri, TValue value, CancellationToken cancellationToken); public static Task<HttpResponseMessage> PatchAsJsonAsync<TValue>(this HttpClient client, Uri? requestUri, TValue value, CancellationToken cancellationToken); public static Task<HttpResponseMessage> PatchAsJsonAsync<TValue>(this HttpClient client, string? requestUri, TValue value, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default); public static Task<HttpResponseMessage> PatchAsJsonAsync<TValue>(this HttpClient client, Uri? requestUri, TValue value, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default); } }

API Usage

var client = new HttpClient(); client.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json"); var body = new { Sha = "sha" }; var response = await client.PatchAsJsonAsync("https://api.github.com/repos/octocat/hello-world/git/refs/REF", body); if (response.IsSuccessfulStatusCode) { Console.WriteLine("Patched!"); }

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Labels

Cost:SWork that requires one engineer up to 1 weekTeam:LibrariesUser StoryA single user-facing feature. Can be grouped under an epic.api-approvedAPI was approved in API review, it can be implementedapi-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Text.Json

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions