从返回的葡萄API字符串中删除引号



我想从我的葡萄/rest api返回原始数据/斑点。

我遵循以下线程:https://github.com/intridea/grape/issues/412

对于以下代码:

get 'foo' do
  content_type 'text/plain'
  "hello world"
end

1)我用过:格式'txt' - 我得到的引用文字,例如:" Hello World"但是,浏览器没有错误,curl给出内容类型:文本/平原,但报价未删除

2)env ['api.format'] =:txt在浏览器中给出错误

3)content_type:txt,'text/plain'在浏览器中给出错误的ARG数量

其他方法可以解决此问题?

谢谢。

您不需要使用'身体',剩下要做的就是在API类中添加一行(上方此方法):

content_type :txt, 'text/plain'

使葡萄使用:用于提供文本/普通内容

的所有端点的TXT格式化器

这是对我有用的东西:

get 'foo' do
  content_type 'text/plain'
  env['api.format'] = :binary
  body 'Stuff here'
end

文档说:

env ['api.format'] =:二进制#没有格式化:二进制,数据将返回"

,只要您不覆盖:binary格式化器,就应该可以。

根据这一点,您可以执行以下操作:

class API < Grape::API
  get 'foo' do
    content_type 'text/plain'
    body 'hello world'
  end
end

使用该方法上方的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

最新更新