gpt4 book ai didi

ruby - 如何让 Ruby 的 RestClient gem 在发布时尊重 content_type?

转载 作者:数据小太阳 更新时间:2023-10-29 06:40:04 24 4
gpt4 key购买 nike

例如,在 RestClient 控制台中:

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

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

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

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

  class UrlEncoded < Base
...

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

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

最佳答案

您可能希望将 json 作为字符串而不是散列作为您的有效载荷。例如,做:

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

如果您查看 payload.rb,它表明如果有效载荷是字符串,它将使用 Base 类而不是 UrlEncoded 类。尝试一下,看看是否适合您。

关于ruby - 如何让 Ruby 的 RestClient gem 在发布时尊重 content_type?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8189932/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com