I want to return raw data/blob from my grape/rest api.
我想从我的grape / rest api返回原始数据/ blob。
I followed the thread at : https://github.com/intridea/grape/issues/412
我跟着线程:https://github.com/intridea/grape/issues/412
for a code like :
对于像这样的代码:
get 'foo' do
content_type 'text/plain'
"hello world"
end
1) I used : format 'txt' - I got quoted text like : "hello world" no error on the browser though, curl gives Content-Type: text/plain but the quotes are not removed
1)我用过:格式'txt' - 我引用的文字如下:“hello world”浏览器没有错误,curl给出了Content-Type:text / plain但引号没有被删除
2) env['api.format'] = :txt gives error in browser
2)env ['api.format'] =:txt在浏览器中出错
3) content_type :txt, 'text/plain' gives error in browser wrong number of args
3)content_type:txt,'text / plain'在浏览器错误的args数中给出错误
Any other ways to fix this ?
还有其他方法可以解决这个问题
Thanks.
3 个解决方案
#1
1
Using content_type :txt, 'text/plain'
above the method and using the body
method worked for me. Here is my code snippet:
使用content_type:txt,方法上方的'text / plain'并使用body方法为我工作。这是我的代码片段:
content_type :txt, "text/plain" desc "ping pong" get "/ping" do challenge = params["hub.challenge"] challenge = "pong" if challenge.to_s.empty? status 200 body challenge end
content_type:txt,“text / plain”desc“ping pong”get“/ ping”do challenge = params [“hub.challenge”] challenge =“pong”if challenge.to_s.empty?状态200身体挑战结束
#2
0
According to this you can do the following:
根据这个你可以做到以下几点:
class API < Grape::API
get 'foo' do
content_type 'text/plain'
body 'hello world'
end
end
#3
0
You don't need to use 'body', all that is left to do is to add one line in the API class (above this method):
您不需要使用'body',剩下要做的就是在API类中添加一行(在此方法之上):
content_type :txt, 'text/plain'
So that Grape uses :txt formatter for all endpoints that serve text/plain content
这样Grape就可以为所有提供文本/纯文本内容的端点使用:txt格式化程序
#1
1
Using content_type :txt, 'text/plain'
above the method and using the body
method worked for me. Here is my code snippet:
使用content_type:txt,方法上方的'text / plain'并使用body方法为我工作。这是我的代码片段:
content_type :txt, "text/plain" desc "ping pong" get "/ping" do challenge = params["hub.challenge"] challenge = "pong" if challenge.to_s.empty? status 200 body challenge end
content_type:txt,“text / plain”desc“ping pong”get“/ ping”do challenge = params [“hub.challenge”] challenge =“pong”if challenge.to_s.empty?状态200身体挑战结束
#2
0
According to this you can do the following:
根据这个你可以做到以下几点:
class API < Grape::API
get 'foo' do
content_type 'text/plain'
body 'hello world'
end
end
#3
0
You don't need to use 'body', all that is left to do is to add one line in the API class (above this method):
您不需要使用'body',剩下要做的就是在API类中添加一行(在此方法之上):
content_type :txt, 'text/plain'
So that Grape uses :txt formatter for all endpoints that serve text/plain content
这样Grape就可以为所有提供文本/纯文本内容的端点使用:txt格式化程序