Rack::Render format being badly inferred from query string
joaodrp opened this issue · comments
Hi,
I have a goliath API which supports both JSON and XML rendering using:
use Goliath::Rack::Render, ['json', 'xml']
It works great. Although I need to support query strings in my API to filter results. My API manages image files, thus it needs to respond to something like:
GET myapi.com/images?name=something&format=jpeg
The problem is that if I do include a filter for images based on their format (format=jpeg), the API messes up with the request output making it unreadable for JSON/XML parsers.
I think that if it finds a query parameter named "format", it guesses the wanted output format from its value rather than from the Accept header only. I've tried to query for "&format=json" and the output comes as JSON. But obviously I need to query images by their (jpeg, png, ...).
Correct: https://github.com/postrank-labs/goliath/blob/master/lib/goliath/rack/render.rb#L41
You can manually set your headers when you return the response and that should get passed through just fine.
Manually set what headers? Content-Type?
by now the last API line is this:
[200, {}, {:images => metadata}]
Umm, confused.. So, you need to return a json / xml response, correct? If so, (a) can you use a different parameter instad of format to pass in the image type? If not, you can either try (a) deleting the format key from env hash before returning the response, or (b) manually providing the Content-Type header.
Solved with
env.params.delete('format')
before the return