Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 将curl命令转换为httparty_Ruby On Rails_Curl_Mailchimp - Fatal编程技术网

Ruby on rails 将curl命令转换为httparty

Ruby on rails 将curl命令转换为httparty,ruby-on-rails,curl,mailchimp,Ruby On Rails,Curl,Mailchimp,我试图在Mailchimp V3列表中添加带有HTTParty的合并字段,但无法将curl转换为HTTParty格式。 工作正常的Curl请求格式: curl --request POST \ --url 'https://usxx.api.mailchimp.com/3.0/lists/17efad7sd4/merge-fields' \ --user '12:d1c1d99dr5000c63f0f73f64b88e852e-xx' \ --header 'con

我试图在Mailchimp V3列表中添加带有HTTParty的合并字段,但无法将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
缺少错误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' } )

我也尝试不使用include选项,但不起作用

您的代码中有几个错误

  • curl
    user
    是基本的身份验证用户,但您正在请求的有效负载中传递它
  • 数据是有效负载,而是将其作为有效负载中的节点传递,然后对其进行双重序列化
  • 包含在那里没有意义,它不是有效负载项
这应该是正确的版本。请花点时间阅读
HTTParty
curl
文档,并理解它们之间的区别

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
)

感谢Simone Carletticurl->httparty converter将是史诗般的。