I have an API endpoint that is supposed to receive either JSON or form urlencoded data. I can detect this by the accept header but I am just wondering if there is a rack middleware that does this for me already. Basically, it converts the parameter based on the accept header.
Asked
Active
Viewed 184 times
1 Answers
2
You want https://github.com/achiu/rack-parser with it you can declare the parsing strategies:
use Rack::Parser, :parsers => {
'application/json' => proc { |body| MyCustomJsonEngine.do_it body },
'application/xml' => proc { |body| MyCustomXmlEngine.decode body },
'application/roll' => proc { |body| 'never gonna give you up' }
}
However, it uses the content_type header. Because that's the correct header to put incoming mime type information. The Accepts header is what the user will accept as a response.

DiegoSalazar
- 13,361
- 2
- 38
- 55