我正在尝试使用 Sinatra 创建一条仅接受带有 Content-type: application/json
的 POST 的路由,但没有成功。
我的做法如下:
post '/dogs', :provides => :json do
# returns here a json response
end
使用 curl 进行测试,我发现 :provides => :json
配置路由以响应 Content-Type: application/json
。
没错,因为我还想用 JSON 消息响应 POST 请求,但我真的需要这条路由只用 Content-Type: application/json
响应 POST 请求,而不是,例如,对其他人(例如 Content-Type: application/xml
)。
在 Sinatra 中有什么方法可以限制路由只接受具有特定 Content-Type
的请求吗?
最佳答案
请求不包含“Content-Type” header ,而是包含“Accept”。 Sinatra 应该基本上只响应包含“application/json”的“Accept”请求。只是为了确保:
post '/gods', :provides => :json do
pass unless request.accept? 'application/json'
...
end
关于ruby - 使用 Sinatra 创建路由以仅接受特定的内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8225689/