Frage

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.

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top