-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add JSON extension methods for HttpClient #33566
Comments
#nullable enable
namespace System.Net.Http.Json {
public static class HttpClientJsonExtensions {
public static Task<object> GetFromJsonAsync(
this HttpClient client,
string? requestUri,
Type type,
CancellationToken cancellationToken);
public static Task<object> GetFromJsonAsync(
this HttpClient client,
string? requestUri,
Type type,
JsonSerializerOptions? options = null,
CancellationToken cancellationToken = default);
public static Task<object> GetFromJsonAsync(
this HttpClient client,
Uri? requestUri,
Type type,
CancellationToken cancellationToken);
public static Task<object> GetFromJsonAsync(
this HttpClient client,
Uri? requestUri,
Type type,
JsonSerializerOptions? options = null,
CancellationToken cancellationToken = default);
public static Task<TValue> GetFromJsonAsync<TValue>(
this HttpClient client,
string? requestUri,
CancellationToken cancellationToken);
public static Task<TValue> GetFromJsonAsync<TValue>(
this HttpClient client,
string? requestUri,
JsonSerializerOptions? options = null,
CancellationToken cancellationToken = default);
public static Task<TValue> GetFromJsonAsync<TValue>(
this HttpClient client,
Uri? requestUri,
CancellationToken cancellationToken);
public static Task<TValue> GetFromJsonAsync<TValue>(
this HttpClient client,
Uri? requestUri,
JsonSerializerOptions? options = null,
CancellationToken cancellationToken = default);
public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(
this HttpClient client,
string? requestUri,
TValue value,
CancellationToken cancellationToken);
public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(
this HttpClient client,
string? requestUri,
TValue value,
JsonSerializerOptions? options = null,
CancellationToken cancellationToken = default);
public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(
this HttpClient client,
Uri? requestUri,
TValue value,
CancellationToken cancellationToken);
public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(
this HttpClient client,
Uri? requestUri,
TValue value,
JsonSerializerOptions? options = null,
CancellationToken cancellationToken = default);
public static Task<HttpResponseMessage> PutAsJsonAsync<TValue>(
this HttpClient client,
string? requestUri,
TValue value,
CancellationToken cancellationToken);
public static Task<HttpResponseMessage> PutAsJsonAsync<TValue>(
this HttpClient client,
string? requestUri,
TValue value,
JsonSerializerOptions? options = null,
CancellationToken cancellationToken = default);
public static Task<HttpResponseMessage> PutAsJsonAsync<TValue>(
this HttpClient client,
Uri? requestUri,
TValue value,
CancellationToken cancellationToken);
public static Task<HttpResponseMessage> PutAsJsonAsync<TValue>(
this HttpClient client,
Uri? requestUri,
TValue value,
JsonSerializerOptions? options = null,
CancellationToken cancellationToken = default);
}
public static class HttpContentJsonExtensions {
public static Task<object> ReadFromJsonAsync(
this HttpContent content,
Type type,
JsonSerializerOptions? options = null,
CancellationToken cancellationToken = default);
public static Task<T> ReadFromJsonAsync<T>(
this HttpContent content,
JsonSerializerOptions? options = null,
CancellationToken cancellationToken = default);
}
public class JsonContent : HttpContent {
public static JsonContent Create<T>(
T value,
MediaTypeHeaderValue mediaType,
JsonSerializerOptions? options = null);
public static JsonContent Create(
object? inputValue,
Type type,
MediaTypeHeaderValue mediaType,
JsonSerializerOptions? options = null);
public Type ObjectType { get; }
public object? Value { get; }
}
} |
I was recently working with some REST-APIs and what I came across a lot was that next to Another thing that I came across often, is that there are Types used for success and failure responses (Type with nullable error objects), so I am wondering if it makes sense to have an overload on As for JsonContent, what will happen, if someone casts the |
Duplicate of #32937 |
We're on a really tight deadline to hit Blazor release in May. I don't think we have the time to add these overloads. FWIW, we considered them but that they seemed rare, but if folks ask, we can add them. So they are still on the table for .NET 5.
We could, but my hunch is that this is going into the long tail of possible tweaks. If you need this, you should call the
|
The full spec is available here.
Usage
Getting data from and to the server is a one-liner
Dealing with HTTP responses is also still doable
Constructing HTTP requests is still doable
Proposed APIs
Assembly: System.Net.Http.Json (new)
Dependencies: System.Net.Http, System.Text.Json
NuGet Package: System.Net.Http.Json (new)
The text was updated successfully, but these errors were encountered: