简体   繁体   English

将curl命令转换为httparty

[英]Convert curl command to httparty

I am trying to add merge field in Mailchimp V3 list with HTTParty but not able to convert curl to HTTParty format. 我正在尝试使用HTTParty在Mailchimp V3列表中添加合并字段,但无法将curl转换为HTTParty格式。
Curl Request format which is working fine : 卷曲请求格式,效果很好:

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 format with error API key missing 缺少错误API密钥的Httparty格式

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' } )

I also try it without include option but not working 我也尝试了没有包含选项但无法正常工作

There are several errors in your code. 您的代码中有几个错误。

  • curl user is the basic auth user, but you are passing it in the payload of the request curl user是基本的auth用户,但是您正在将其传递到请求的有效负载中
  • data is the payload, instead you are passing it as a node in your payload and then you double serialize it 数据是有效负载,而是将其作为有效负载中的节点传递,然后对它进行双重序列化
  • include makes no sense there, it's not a payload item 包含在那里没有意义,这不是有效载荷项目

This should be the correct version. 这应该是正确的版本。 Please take a moment to read the HTTParty and curl documentation and understand the differences. 请花一点时间阅读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
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM