mmazi / rescu

A lightweight json Rest client utility for JAX-RS
MIT License
65 stars 60 forks source link

Is there a way to handle headers for empty response? #132

Open sizmestiev opened 3 years ago

sizmestiev commented 3 years ago

Hello Matija.

I have interaction with an exchange which sends authentication information in the headers of empty response.

Both JSON and plain text readers are unable to create HttpResponseAware object for empty responses - they just return null: https://github.com/mmazi/rescu/blob/55a60966f1aa1ce81a83a8ba50d06aabe42c84d1/src/main/java/si/mazi/rescu/ResponseReader.java#L62

I wonder - is there a way to receive any object for such empty responses to handle the response headers?

Sample response:

Request http status = 200
Header response property: key='Server', value='[nginx/1.18.0]'
Header response property: key='X-Content-Type-Options', value='[nosniff, nosniff]'
Header response property: key='Connection', value='[keep-alive]'
Header response property: key='Pragma', value='[no-cache]'
Header response property: key='Referrer-Policy', value='[same-origin]'
Header response property: key='X-Frame-Options', value='[SAMEORIGIN, DENY]'
Header response property: key='Auth', value='XXX'
Header response property: key='Cache-Control', value='[no-cache, no-store, max-age=0, must-revalidate]'
Header response property: key='Expires', value='[0]'
Header response property: key='X-XSS-Protection', value='[1; mode=block, 1; mode=block]'
Header response property: key='Content-Length', value='[0]'
Http call returned 200; response body:
mmazi commented 3 years ago

Thinking aloud here. As an empty string is not a valid JSON, JacksonResponseReader's behavior is probably correct in this case. In the case of a text/plain response it would make sense to return an empty String when the response body is empty. However, String cannot implement HttpResponseAware. So I'm not sure how to proceed.

What is the declared content type of the response?

mmazi commented 3 years ago

Oh actually JacksonResponseReader's behavior is not correct with the empty string -- it should probably throw an exception (invalid json).

Anyhow, this probably doesn't help much either.

What is the declared content type of the response?

I see now that no content type is declared in the response.

sizmestiev commented 3 years ago

Is it possible to add ability to specify a custom ResponseReader in addition to Jackson and text/plain ?

mmazi commented 3 years ago

Is it possible to add ability to specify a custom ResponseReader in addition to Jackson and text/plain ?

Currently, no. I see you've delved into rescu source code -- if you create a pull request implementing this ability, I'll be happy to review.

A workaround/hack might exist for your issue though. Exceptions can implement HttpResponseAware, so if you manage to have an exception thrown when processing the response you might be able to get the headers. Unfortunately I don't see an easy way to do this as this is generally only triggered when the response body cannot be parsed or when the http response code is not 2xx. It would help if the code you linked were change to not return null on empty response and try to parse it as json (empty string is illegal json), but I'm afraid that might break many existing implementations that rely on the current behavior.

sizmestiev commented 3 years ago

if you create a pull request implementing this ability, I'll be happy to review.

Thank you.
I don't have enough time now to implement the ability carefully, but this functionality is extremely important to me.
I'll do my best when I'm ready.