edgurgel / httparrot

HTTP Request & Response Server. An incomplete clone of http://httpbin.org
https://httparrot.onrender.com/
MIT License
86 stars 27 forks source link

Add /response-headers endpoint #13

Closed flupke closed 9 years ago

edgurgel commented 9 years ago

Hey @flupke, thanks for the contribution!

We still need to reply with a JSON response which includes the headers like httpbin.org does:

http://httpbin.org/response-headers?Content-Type=text/plain;%20charset=UTF-8&Server=httpbin

TBH I'm not sure how to achieve this using Cowboy as we need to access the final headers and pretty print them using JSON.

What do you think?

flupke commented 9 years ago

I didn't find how to retrieve cowboy's default response headers either.

Maybe we could just return the json of the query string parameters? I don't see what meaningful test would check the automatically generated headers anyway.

edgurgel commented 9 years ago

Yeah, maybe it's good enough to return the requested parameters :+1:

Then we can find later a solution to return every response header.

flupke commented 9 years ago

There you go

edgurgel commented 9 years ago

Thanks!

jordan0day commented 9 years ago

First off, thanks for adding this, I was just about to go down the path of adding this to HTTParrot myself, so glad to see you've done it already.

With regards to the issue of JSONifying the real list of response headers, HTTParrot.prettify_json/4 will have access to the full list of response headers (as it's set as the "onresponse" handler for cowboy), including those headers that Cowboy itself adds. Unfortunately, since prettify_json/4 is called for every request, and not just the /response_headers request, it seems like you'd have to add some extra logic to handle just this particular case. I can't think of a way to do this very elegantly.

I would like to point out, however, that in this case, the HTTParrot implementation is better than the original HTTPbin version. At least with HTTParrot, if I send a querystring like: http://localhost:4000/response-headers?Server=httpbin, the response headers I receive back contain:

server:Cowboy
server:httpbin

Whereas the same request to httpbin only returns: Server:nginx (it seems httpbin can't even set a custom server header)