You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[System.Net.Http.Json] Post method miss overloads for reading content #34157
technically yes. But overall idea if I understand correctly is to GET/POST/PUT date in simple fashion. It was quite confusing migrate from Preview 2. Maybe worth mentioning in the upgrade guide if this is intended.
I think that when you send a Post request, the server does not necessarily will respond with a JSON so I don't know how common is that scenario.
Servers rely on HTTP request headers to decide what format to send back as the response. The 'Accept' request header will specify something like 'application/json' when it sends the request to the server. Then the server knows what is suitable to send back to the client.
I think that when you send a Post request, the server does not necessarily will respond with a JSON so I don't know how common is that scenario.
Using the previous APIs (PostJsonAsync), what happens when the response's content-type is not application/json?
I believe most of these convenience APIs assume the server will behave in a particular way and then throw when it doesn't. Same case could be true with a Get. If we can make these APIs be overloads (and not ambiguous) then it seems a reasonable request.
Why were they omitted? I remember discussion around this but can't recall the conclusion.
Many (most?) JSON APIs support some sort of error feedback that is not directly encoded in a successful response's object model -- either using e.g. "404" response with no content, or still JSON but with a different object model.
We discussed this briefly in API review and decided to leave it for future design. I think the challenge is to identify strong use cases for this.
Many (most?) JSON APIs support some sort of error feedback that is not directly encoded in a successful response's object model -- either using e.g. "404" response with no content, or still JSON but with a different object model.
Doesn't that same concern apply to all Get methods we already expose?
Doesn't that same concern apply to all Get methods we already expose?
Yes. I would not recommend using them outside of very trivial APIs or in toy code.
99% of the time I'd use something that captures the data those methods throw away in the non-success paths:
asyncTask<SuccessResponseObject?>ProcessResponse(HttpResponseMessageresponse){if(response.IsSuccessStatusCode){returnawaitresponse.Content.ReadAsAsync<SuccessResponseObject>();}if(response.StatusCode==404){returnnull;}if(response.StatusCode==Some500StatusWellDefinedByAPI){// might contain more specific error messages, a model dictionary of validation errors, etc.varerrorInfo=awaitresponse.Content.ReadAsAsync<ErrorResponseObject>();throwCreateExceptionFromErrorInfo(errorInfo);}throwCreateUnknownError(response.StatusCode);}
@jozkee@terrajobst can you please have a look here and consider @rynowak's analysis? This is precisely the type of feedback we're looking to consider with our preview of this package.
If we want to talk about this more, I'm game. I also want to amend something I said (based on re-reading).
It's somewhat bizzare that an API would return a value on failure but not on success
I think I was wrong about this. Consider the case of a DELETE. I think there are probably plenty of cases where you return nothing on success, but have the potential to return an error object on failure.
As mentioned in #32937 (comment), we might not need TError on methods that does not directly return a HttpResponsemessage, instead we should throw on responses that are not 2xx; that is how GetFromJsonAsync works.
For what is worth, having this set of APIs could also provide an alternative for users from having to dispose the HttpResponseMessage themselves. #33459 (comment)
changed the title [-]System.Net.Http.Json Post method miss overloads for reading content[/-][+][Json] System.Net.Http.Json Post method miss overloads for reading content[/+]on May 6, 2020
changed the title [-][Json] System.Net.Http.Json Post method miss overloads for reading content[/-][+][System.Net.Http.Json] Post method miss overloads for reading content[/+]on Jan 5, 2021
Activity
davidsh commentedon Mar 26, 2020
@karelz @jozkee
stephentoub commentedon Mar 26, 2020
?
kant2002 commentedon Mar 26, 2020
technically yes. But overall idea if I understand correctly is to GET/POST/PUT date in simple fashion. It was quite confusing migrate from Preview 2. Maybe worth mentioning in the upgrade guide if this is intended.
jozkee commentedon Mar 26, 2020
So the ask here is to make
PostAsJsonAsyncandPutAsJsonAsyncmethods to directly deserialize theHttpResponseMessage's content?I think that when you send a Post request, the server does not necessarily will respond with a JSON so I don't know how common is that scenario.
Using the previous APIs (
PostJsonAsync), what happens when the response's content-type is notapplication/json?@rynowak @terrajobst
davidsh commentedon Mar 26, 2020
Servers rely on HTTP request headers to decide what format to send back as the response. The 'Accept' request header will specify something like 'application/json' when it sends the request to the server. Then the server knows what is suitable to send back to the client.
kant2002 commentedon Mar 27, 2020
For me this API appear half-backed.
Previous
HttpClient. This make sense. At least this was explicitly stated in public announcements.Current
All of that can be just out of confusion how to properly use API and for what scenarios new API intended.
ericstj commentedon Mar 31, 2020
I believe most of these convenience APIs assume the server will behave in a particular way and then throw when it doesn't. Same case could be true with a Get. If we can make these APIs be overloads (and not ambiguous) then it seems a reasonable request.
Why were they omitted? I remember discussion around this but can't recall the conclusion.
scalablecory commentedon Mar 31, 2020
Many (most?) JSON APIs support some sort of error feedback that is not directly encoded in a successful response's object model -- either using e.g. "404" response with no content, or still JSON but with a different object model.
We discussed this briefly in API review and decided to leave it for future design. I think the challenge is to identify strong use cases for this.
ericstj commentedon Apr 1, 2020
Doesn't that same concern apply to all Get methods we already expose?
scalablecory commentedon Apr 1, 2020
Yes. I would not recommend using them outside of very trivial APIs or in toy code.
99% of the time I'd use something that captures the data those methods throw away in the non-success paths:
2 remaining items
ericstj commentedon Apr 8, 2020
@jozkee @terrajobst can you please have a look here and consider @rynowak's analysis? This is precisely the type of feedback we're looking to consider with our preview of this package.
rynowak commentedon Apr 8, 2020
If we want to talk about this more, I'm game. I also want to amend something I said (based on re-reading).
I think I was wrong about this. Consider the case of a
DELETE. I think there are probably plenty of cases where you return nothing on success, but have the potential to return an error object on failure.jozkee commentedon Apr 8, 2020
As mentioned in #32937 (comment), we might not need
TErroron methods that does not directly return aHttpResponsemessage, instead we should throw on responses that are not 2xx; that is howGetFromJsonAsyncworks.jozkee commentedon Apr 8, 2020
As @rynowak suggested, this could be shaped as the following (extended to the remaining overloads):
The implementation is probably going to look something like this:
PutmethodsTValuetoTRequestin the existingPostandPutmethodsTResponsein theGetmethods.All of this assuming that we agree on adding this set of APIs.
jozkee commentedon Apr 8, 2020
For what is worth, having this set of APIs could also provide an alternative for users from having to dispose the
HttpResponseMessagethemselves. #33459 (comment)[-]System.Net.Http.Json Post method miss overloads for reading content[/-][+][Json] System.Net.Http.Json Post method miss overloads for reading content[/+]SteveSandersonMS commentedon Sep 28, 2020
@velocitytoo You can send and receive different types:
[-][Json] System.Net.Http.Json Post method miss overloads for reading content[/-][+][System.Net.Http.Json] Post method miss overloads for reading content[/+]aradalvand commentedon Oct 7, 2023
I agree that this should be added; I was surprised to find out that no such overload exists already.
WeihanLi commentedon May 20, 2024
+1, think this would be greatly useful when using typed JSON request and response
and please also add support for PUT and PATCH when considering POST