0

I'm working on a simple API with Sinatra and I have a route like this one:

get '/api/v1/invoice/:biller', :provides => [:json] do
   respond_with invoice( request )
end

It works like a charm when I don't send any header params, but when I send:

  • Accept
  • Content-Type

Then I got a 404 Not Found error and the classic Sinatra error 'Sinatra doesn't know this ditty'.

How can I validate specific header params on Sinatra?

Edit

This is the actual header (Accept) with a curl example:

curl -H "Accept: application/vnd.tpago.billpayment+json" -X GET "http://localhost:3540/api/v1/invoice/5947647"

Thanks!

mcKain
  • 437
  • 5
  • 16
  • 1
    Could you edit to add the actual headers you used, with their value. Or even better, a `curl` command line or similar to show how you are sending them. You shouldn't need to set `Content-Type` for a GET request, there is no content – Neil Slater Nov 15 '13 at 14:53
  • I added the curl line! Thanks! – mcKain Nov 15 '13 at 15:08
  • Does `curl -H "Accept: application/json"` work? I'm not sure Sintra's `:provides` condition knows about your media type . . . – Neil Slater Nov 15 '13 at 15:15

2 Answers2

3

If you change your request to:

curl -H "Accept: application/json" -X GET "http://localhost:3540/api/v1/invoice/5947647"

It will work as Neil suggest or if you modify your Sinatra app to:

configure do
  # include new mime type
  mime_type :tpago, 'application/vnd.tpago.billpayment'
end

# add into provide options
get '/api/v1/invoice/:biller', :provides => [:tpago,:json] do
   respond_with invoice( request )
end

Now, the following request will work:

curl -H "Accept: application/vnd.tpago.billpayment" -X GET "http://localhost:3540/api/v1/invoice/5947647"

I am not totally sure, but I think "+" sign doesn't work on Accept header. I didn't find any reference about it on w3 documentation.

Thiago Lewin
  • 2,810
  • 14
  • 18
0

Thanks tlewin!

I applied your solution but then I discovered an issue with 'respond_with'. It was always returning a 408 Not Acceptable so I went to the Sinatra's documentation, so I used a respond_to:

get '/api/v1/invoice/:biller', :provides => [:tpago,:json] do
  result = invoice( request )
  respond_to do |f|
    f.on('application/vnd.tpago.billpayment+json') { result.to_json }
  end
end

And now it's working! :D

Thank you again!

mcKain
  • 437
  • 5
  • 16