【发布时间】:2019-06-11 10:20:29
【问题描述】:
上下文:
我正在使用 HTTParty 在接受 application/json 的 API 上执行 POST 请求
我的身体是这样的(作为哈希)
{:contact_id =>"12345679",
:items=> [
{:contact_id=>"123456789", :quantity=>"1", :price=>"0"},
{:contact_id=>"112315475", :quantity=>"2", :price=>"2"}
]}
问题:
当我编写以下代码时,这是有效的:
HTTParty.post('https://some-url', body: content.to_json, headers: { "Content-Type" => "application/json" } )
但是当仅将标题中的=> 符号更改为: 符号时,这不起作用(API 响应某些参数丢失)
HTTParty.post('https://some-url', body: content.to_json, headers: { "Content-Type": "application/json" } )
问题:
为什么将"Content-Type" => "application/json" 更改为"Content-Type": "application/json" 会导致错误?
我认为这是我不理解的 Ruby 哈希。
编辑:
我认为我的问题与Why is this string key in a hash converted to a symbol?不重复
对于 HTTParty 消费者来说,了解 HTTParty 不接受标头的符号只接受字符串,这一点很重要。
有关详细信息,请参阅 Content-Type Does not send when I try to use a new Hash syntax on header 和 Passing headers and query params in HTTparty
谢谢@anothermh
【问题讨论】: