【问题标题】:Convert curl command to httparty将 curl 命令转换为 httparty
【发布时间】:2016-06-08 07:56:49
【问题描述】:

我正在尝试使用 HTTParty 在 Mailchimp V3 列表中添加合并字段,但无法将 curl 转换为 HTTParty 格式。
运行良好的卷曲请求格式:

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

我也尝试了不包含选项但不工作的方法

【问题讨论】:

    标签: ruby-on-rails curl mailchimp


    【解决方案1】:

    您的代码中有几个错误。

    • curl user 是基本身份验证用户,但您在请求的负载中传递它
    • data 是有效负载,而是将其作为有效负载中的节点传递,然后将其双重序列化
    • 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
    )
    

    【讨论】:

    • 谢谢西蒙娜·卡莱蒂
    • curl -> httparty 转换器将是史诗般的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    相关资源
    最近更新 更多