我如何让 Ruby 的 RestClient gem 在 post 上尊重 content_type? [英] How do I make Ruby's RestClient gem respect content_type on post?

查看:56
本文介绍了我如何让 Ruby 的 RestClient gem 在 post 上尊重 content_type?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在 RestClient 控制台中:

For instance, in the RestClient console:

RestClient.post 'http://localhost:5001', {:a => 'b'}, :content_type => 'application/json'

这不会将 application/json 作为内容类型发送.相反,我看到:

This does not send application/json as the content type. Instead I see:

Content-Type: application/x-www-form-urlencoded

我能够追踪到 restclient/payload.rb 的变化:

I was able to trace the change to restclient/payload.rb:

  class UrlEncoded < Base
  ...

  def headers
    super.merge({'Content-Type' => 'application/x-www-form-urlencoded'})
  end
end

用 super 替换 super.merge 会导致尊重内容类型,但显然这不是真正的解决方案.有谁知道解决这个问题的正确方法?谢谢.

Replacing super.merge with super causes the content type to be respected, but obviously that's not a real solution. Does anyone know the proper way to fix this? Thanks.

推荐答案

您可能希望将 json 作为字符串而不是哈希作为负载.例如,执行:

You might want to put json as string as your payload instead of hash. For example, do:

RestClient.post 'http://localhost:5001','{"a":"b"}',:content_type => 'application/json'

如果您查看payload.rb,它表明如果payload 是字符串,它将使用Base 类而不是UrlEncoded 类.试试看,看看它是否适合你.

If you look at the payload.rb, it shows that it will use the Base clase instead of UrlEncoded class if the payload is string. Try that and see if that work for you.

这篇关于我如何让 Ruby 的 RestClient gem 在 post 上尊重 content_type?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆