将 curl 命令转换为 httparty

Convert curl command to httparty

我正在尝试使用 HTTParty 在 Mailchimp V3 列表中添加合并字段,但无法将 curl 转换为 HTTParty 格式。
工作正常的 Curl 请求格式:

curl --request POST \
     --url 'https://usxx.api.mailchimp.com/3.0/lists/17efad7sd4/merge-fields' \
     --user '12:d1c1d99dr5000c63f0f73f64b88e852e-xx' \
     --header 'content-type: application/json' \
     --data '{"name":"FAVORITEJOKE", "type":"text"}' \
     --include

Httparty 格式错误 API 密钥丢失

response = HTTParty.post("https://us12.api.mailchimp.com/3.0/lists/17efad7sde/merge-fields", 
                :body => { 
                  :user => '12:d1c1d99dr5000c63f0f73f64b88e852e-xx',
                  :data =>  '{"name":"FAVORITEJOKE", "type":"text"}',
                  :include => ''
                }.to_json,
                :headers => { 'Content-Type' => 'application/json' } )

我也尝试过不包含选项但无法正常工作

您的代码中有几个错误。

  • curl user 是基本的 auth 用户,但是你在请求的有效负载中传递它
  • 数据是有效载荷,而不是将其作为有效载荷中的节点传递,然后将其双重序列化
  • include 在那里没有意义,它不是有效负载项

这应该是正确的版本。请花点时间阅读 HTTPartycurl 文档并了解差异。

HTTParty.post(
  "https://us12.api.mailchimp.com/3.0/lists/17efad7sde/merge-fields", 
  basic_auth: { username: "12", password: "d1c1d99dr5000c63f0f73f64b88e852e-xx" },
  headers: { 'Content-Type' => 'application/json' },
  body: {
    name: "FAVORITEJOKE",
    type: "text",
  }.to_json
)