Closed barnabeliqueux closed 2 months ago
The purpose of that method is to let you the consumer decide how to handle the response and map it into something else. It is purposely "raw" and does not make assumptions about what the customer wants to do or how to behave. The response might not even be a string, and you might not be mapping it into a DTO (the 'Object' could be a boolean, a File, whatever. There could even be no "parsing" done at all!
Unirest has no idea what you are going to do in that function, unlike the class based one where we are explicitly using the object mapper and the response as a string (for now, that will likely change in a future version in order to better support binary payloads like Protobuff). In this case all of the logic for parsing and what that means is up to you. You can try/catch inside the function and do whatever you want.
Another possible way to handle it is to use a custom ObjectMapper where you override readValue and you can intercept the string before passing it up to super.readValue
I understand, thank you for this explanation !
Hi, I recently needed to obtain the raw String body to perform some database storage, before it was parsed into a POJO, for which i was using the
asObject(Class<? extends T> var1)
(let us call it the class one) method of theHttpRequest
class.I came across the method
asObject(Function<RawResponse, T> var1)
(let us call it the function one) which seemed to fill my purpose as it provides a RawResponse that i can manipulate to perform my actions, before returning the parsed body.However I noticed that the function method did not work the same way as the class:
ObjectResponse
for parsing the raw body into a POJO, which will handle parsing errors with a flag, and store the result inside the response.BasicResponse
which will simply callapply
to the function parameter. As a consequence, any parsing error will not be caught, and error handling must be done outside.This causes differences in implementation wether using the class or the function version.
Which leads us to the following feature request : Is it possible to make it so that the function
asObject
works the same way as the class one, so that in the event of a parsing error, set the correct flag in the response without throwing ?If there is a way to perform what I am trying to achieve that I have missed, I would gladly appreciate some advice.
Thank you for your help and your time.