【问题标题】:remove quotes from returned string of grape api从返回的葡萄 api 字符串中删除引号
【发布时间】:2015-07-17 09:48:59
【问题描述】:

我想从我的 grape/rest api 返回原始数据/blob。

我关注了https://github.com/intridea/grape/issues/412的帖子

对于这样的代码:

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

1) 我用过:格式'txt' - 我得到了引用的文字,例如:“hello world” 虽然浏览器没有错误,但 curl 给出 Content-Type: text/plain 但引号没有被删除

2) 环境['api.format'] = :txt 浏览器报错

3) content_type :txt, '文本/纯文本' 在浏览器错误数量的参数中给出错误

还有其他方法可以解决这个问题吗?

谢谢。

【问题讨论】:

    标签: html ruby thin grape grape-api


    【解决方案1】:

    根据this,您可以执行以下操作:

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

    【讨论】:

    • 这不是单引号应该做的伎俩,它的body应该根据文档来做。
    • 我之前包含了错误的 URL。我已经用我想发布的 URL 更新了我的答案,但显然这对你没有用。顺便说一句,该页面上有很多文档。
    • 我也有同样的问题。这方面有什么进展吗?这么简单的事情为什么这么复杂?
    【解决方案2】:

    你不需要使用'body',剩下要做的就是在API类中添加一行(在这个方法之上):

    content_type :txt, 'text/plain'
    

    因此 Grape 对所有提供文本/纯内容的端点使用 :txt 格式化程序

    【讨论】:

      【解决方案3】:

      在方法上方使用content_type :txt, 'text/plain' 并使用body 方法对我有用。这是我的代码 sn-p:

      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

      【讨论】:

        【解决方案4】:

        这对我有用:

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

        The documentation 说:

        env['api.format'] = :binary # :binary 没有格式化程序,数据将“按原样”返回

        因此,只要您不覆盖 :binary 格式化程序,就可以了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-09-18
          • 1970-01-01
          • 1970-01-01
          • 2015-09-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多