Rails API 葡萄 |不支持内容类型
module API
class Root < Grape::API
prefix 'api'
content_type :json, 'application/json'
format :json
default_format :json
rescue_from Grape::Exceptions::ValidationErrors do |e|
error!({ error: [{ msg: 'card's information is incorrect' }] }, 400)
end
rescue_from :all
mount API::Ver1::Poker
end
end
这是我的 API 的根。 如果我请求 JSON 格式的内容,此 API 将返回正确的值。 但是,如果我请求其他格式(例如文本或 XML)的内容,则此 API 会返回如下所示。
{
"error": "The provided content-type 'text/plain' is not supported."
}
或者
{
"error": "The provided content-type 'application/xml' is not supported."
}
我想显示错误消息“卡片信息不正确”。
为什么下面的验证码不起作用?
rescue_from Grape::Exceptions::ValidationErrors do |e|
error!({ error: [{ msg: 'card's information is incorrect' }] }, 400)
end
module API
class Root < Grape::API
prefix 'api'
content_type :json, 'application/json'
format :json
default_format :json
rescue_from Grape::Exceptions::ValidationErrors do |e|
error!({ error: [{ msg: 'card's information is incorrect' }] }, 400)
end
rescue_from :all
mount API::Ver1::Poker
end
end
This is my API's root.
If I request something in JSON format, this API returns correct value.
However, if I request something in other formats such as Text or XML, this API returns like below.
{
"error": "The provided content-type 'text/plain' is not supported."
}
or
{
"error": "The provided content-type 'application/xml' is not supported."
}
I want to show error message 'cards information is incorrect'.
Why does not the following validation code work?
rescue_from Grape::Exceptions::ValidationErrors do |e|
error!({ error: [{ msg: 'card's information is incorrect' }] }, 400)
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您将
content-type
设置为json
,所以它不会接受任何其他content-type
。如果您希望支持所有
content-types
,content type:json,'application/json
行或
详细了解 grape API 格式
Because you set the
content-type
tojson
, so it won't accept any othercontent-type
.If you wish to support all
content-types
content type:json,'application/json
lineOR
Read more about grape API formats