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
4.x add deprecation warning to response.parse() #665
Actually.... I am not sure I understand why this API was changed at all? This change makes the #parse method much less usable. If I know what the content type is, I do not need the HTTP gem to parse it for me - I can JSON.parse it myself.... I was assuming that the intention of the #parse method is to do "best effort" to parse it.
In addition, the response object already knows what the mime type is, so why not use it to parse?
The below code feels wrong:
require'http'url="https://jsonplaceholder.typicode.com/posts/1"response=HTTP.geturl# Why do we have to do this...parsed_response=response.parse(response.mime_type)# ...instead of this?# parsed_response = response.parsepparsed_response
To me, the beauty of the HTTP gem is that it "just works".
Can't the #parse method be changed to have its type argument optional?
Even if you pass Accept: application/json header, server might respond with plain text, or html, etc. So implicit mime type inferring from the response might lead into a confusing experience IMO:
Now you have a parsed response. But it's far from what you expect. So, to be honest I consider parse as a simple helper over JSON.parse(response.to_s), thus I think response.parse(:json) (which can be enhanced to pick up the most efficient adapter) is a better choice.
And let's say you expect result to be a JSON, but it comes back as HTML (error or such):
We can say a lot of things.... 😜
If the response comes back with unexpected content, anything will fail. I don't see the point of this line of questioning. This will also fail with me calling response.parse(:json).
I can understand a case where the Response object does not handle any parsing.
I can understand a case where it uses its own mime type to provide a parsed response "out of the box".
I cannot understand the purpose of a parse "wrapper" that accepts an argument and just calls an adapter. This does not seem like related to the scope of what an HTTP client should do, unless it is being "smart" about it and infers the response type automatically.
Anyways, that is my opinion. Not a big deal from my own standpoint (I switched to JSON.parse), but I feel it is the wrong direction for this particular feature.
Since we are already off topic, i can contribute my 2 cents.
How about if we let parse() be automatic again, but require an allow list to be provided.
So only if the mimetype matches the expected one, automatic parsing takes place.
Sorry, this is probably my bad. I thought we are on topic, since opening another topic on the subject would have conflicted with this one. My on-topic thought was "don't deprecate, revert".
Http.get(url).parse(:json, :xml)
Personally, I do not see the point in that. It is not the responsibility of an HTTP library to provide parsing wrappers. It is the responsibility of a non-bare-bones HTTP library (such as the HTTP gem in my understanding), to provide automatic parsing when it can do so easily and reliably.
I do not know what was the past implementation that prompted the authors to change it, but in my view "automatic" does not mean "make a guess". It simply means "use the information available to you". So, if the response object has a mime type, and if it knows how to parse this mime type, then Response#parse can be used to automatically get the parsed output. Otherwise, calling #parse should result in some friendly UnspecifiedMimeType exception or similar.
I do not think that situations where the server does not return a mime type, or when it returns different mime types for the same endpoint should be the used as a guide for the implementation. These are edge cases of badly behaving servers. If some users face this, they should test for the mime type themselves and act accordingly.
Lastly, I do not see an advantage of calling response.parse :json instead of JSON.parse response. This is just masking a native Ruby behavior behind a method that needs maintaining and explaining.
A closed 2 years old topic? I doubt any reply there would have received any attention, but one thing is certain - discussing whether or not this is on topic, is by itself off topic... :)
Bottom line, your call of course how to handle this. If there is any weight to my opinion, I say either make it parse by mime type (and fail gracefully if it can't), or remove the parse method altogether. Any other route is awkward at best, and bad form at worst.
Activity
tarcieri commentedon May 18, 2021
That's a good idea.
ixti commentedon May 19, 2021
fa54233
DannyBen commentedon May 25, 2021
Actually.... I am not sure I understand why this API was changed at all? This change makes the
#parsemethod much less usable. If I know what the content type is, I do not need the HTTP gem to parse it for me - I canJSON.parseit myself.... I was assuming that the intention of the#parsemethod is to do "best effort" to parse it.In addition, the response object already knows what the mime type is, so why not use it to parse?
The below code feels wrong:
To me, the beauty of the HTTP gem is that it "just works".
Can't the
#parsemethod be changed to have itstypeargument optional?What am I missing?
ixti commentedon May 25, 2021
@DannyBen
Even if you pass
Accept: application/jsonheader, server might respond with plain text, or html, etc. So implicit mime type inferring from the response might lead into a confusing experience IMO:Let's say you define HTML decoder for that:
And let's say you expect result to be a JSON, but it comes back as HTML (error or such):
Now you have a parsed response. But it's far from what you expect. So, to be honest I consider
parseas a simple helper overJSON.parse(response.to_s), thus I thinkresponse.parse(:json)(which can be enhanced to pick up the most efficient adapter) is a better choice.See Also: #538
ixti commentedon May 25, 2021
It's easy enough to revert this API change locally:
You can also overload
HTTP::Response#parsecompletely.DannyBen commentedon May 25, 2021
We can say a lot of things.... 😜
If the response comes back with unexpected content, anything will fail. I don't see the point of this line of questioning. This will also fail with me calling
response.parse(:json).Responseobject does not handle any parsing.parse"wrapper" that accepts an argument and just calls an adapter. This does not seem like related to the scope of what an HTTP client should do, unless it is being "smart" about it and infers the response type automatically.Anyways, that is my opinion. Not a big deal from my own standpoint (I switched to
JSON.parse), but I feel it is the wrong direction for this particular feature.ronald commentedon May 26, 2021
Since we are already off topic, i can contribute my 2 cents.
How about if we let
parse()be automatic again, but require an allow list to be provided.So only if the mimetype matches the expected one, automatic parsing takes place.
DannyBen commentedon May 26, 2021
Sorry, this is probably my bad. I thought we are on topic, since opening another topic on the subject would have conflicted with this one. My on-topic thought was "don't deprecate, revert".
Personally, I do not see the point in that. It is not the responsibility of an HTTP library to provide parsing wrappers. It is the responsibility of a non-bare-bones HTTP library (such as the HTTP gem in my understanding), to provide automatic parsing when it can do so easily and reliably.
I do not know what was the past implementation that prompted the authors to change it, but in my view "automatic" does not mean "make a guess". It simply means "use the information available to you". So, if the response object has a mime type, and if it knows how to parse this mime type, then
Response#parsecan be used to automatically get the parsed output. Otherwise, calling#parseshould result in some friendlyUnspecifiedMimeTypeexception or similar.I do not think that situations where the server does not return a mime type, or when it returns different mime types for the same endpoint should be the used as a guide for the implementation. These are edge cases of badly behaving servers. If some users face this, they should test for the mime type themselves and act accordingly.
Lastly, I do not see an advantage of calling
response.parse :jsoninstead ofJSON.parse response. This is just masking a native Ruby behavior behind a method that needs maintaining and explaining.ronald commentedon May 26, 2021
IMHO on-topic would have been there: #538
DannyBen commentedon May 26, 2021
A closed 2 years old topic? I doubt any reply there would have received any attention, but one thing is certain - discussing whether or not this is on topic, is by itself off topic... :)
Bottom line, your call of course how to handle this. If there is any weight to my opinion, I say either make it parse by mime type (and fail gracefully if it can't), or remove the
parsemethod altogether. Any other route is awkward at best, and bad form at worst.tarcieri commentedon May 26, 2021
If you really liked
parse()with no argument and want it back, I'd suggest opening a PR that reverts #538 and we can discuss it there.ixti commentedon May 26, 2021
I've reverted the deprecation warning in 4.x.x branch, further discussion of
#parsefuture should go in #670