I'm trying to create a route with Sinatra that only accepts POST with an Content-type: application/json
without success.
My approach is as follows:
post '/dogs', :provides => :json do
# returns here a json response
end
Testing with curl, I have seen that :provides => :json
configures the route to respond with an Content-Type: application/json
.
That's right because I want also to respond with a JSON message to the POST request but I really need that this route only respond to POST requests with a Content-Type: application/json
and not, for example, to others (e.g. Content-Type: application/xml
).
Is there any way in Sinatra to restrict the route to only accept requests with a certain Content-Type
?
Requests do not contain "Content-Type" header, but rather have "Accept". Sinatra should basically only respond to requests with "Accept" containing "application/json". Just to make sure:
post '/gods', :provides => :json do
pass unless request.accept? 'application/json'
...
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With