簡體   English   中英

將curl命令轉換為httparty

[英]Convert curl command to httparty

我正在嘗試使用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

缺少錯誤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' } )

我也嘗試了沒有包含選項但無法正常工作

您的代碼中有幾個錯誤。

  • curl user是基本的auth用戶,但是您正在將其傳遞到請求的有效負載中
  • 數據是有效負載,而是將其作為有效負載中的節點傳遞,然后對它進行雙重序列化
  • 包含在那里沒有意義,這不是有效載荷項目

這應該是正確的版本。 請花一點時間閱讀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