如何使用Sinatra验证标头参数? [英] How to validate header parameters with Sinatra?

查看:99
本文介绍了如何使用Sinatra验证标头参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Sinatra开发一个简单的API,并且我有一条像这样的路线:

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:

  • 接受
  • 内容类型

然后我遇到一个404 Not Found错误,而经典的Sinatra错误'Sinatra不知道这个小问题'.

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

如何验证Sinatra上的特定标头参数?

How can I validate specific header params on Sinatra?

修改

这是带有curl示例的实际标头(接受):

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"

谢谢!

推荐答案

如果将请求更改为:

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

它将按照Neil的建议进行操作,或者如果您将Sinatra应用程序修改为:

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"

我不确定,但是我认为Accept标头上的"+"符号无效.我没有在w3文档中找到任何有关它的参考.

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.

这篇关于如何使用Sinatra验证标头参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆