【问题标题】:Sending POST request with array in body with HTTParty使用 HTTParty 在正文中发送带有数组的 POST 请求
【发布时间】:2020-04-22 16:24:08
【问题描述】:

我正在尝试通过 Buttondown API 创建新订阅者。

Docs here.

api 需要以下主体:

{
  "email": "string",
  "notes": "string",
  "referrer_url": "string",
  "tags": [
    "string"
  ]
}

电子邮件字段是必需的,所有其他字段都是可选的。

我可以成功创建一个新订阅者。但是,我无法添加任何标签。

我已经创建了一个名为“marketing”的标签并确认它存在。

我像这样创建一个新订阅者:

  body = {
    'email': params[:email],
    'referrer_url': params[:referer],
    'tags': ['marketing']
  }

  res = HTTParty.post(
          'https://api.buttondown.email/v1/subscribers', 
          body: body,
          headers: headers
        )

我希望以上内容能够创建一个带有 marketing 标签的新订阅者。但是,查看响应,标签还没有进入记录。

{
  "creation_date"=>"2020-04-22T16:06:22.114523Z",
   "email"=>"user@example.com",
   "id"=>"********-****-****-****-************",
   "notes"=>"",
   "referrer_url"=>"http://localhost:3000/",
   "metadata"=>{},
   "secondary_id"=>18,
   "subscriber_type"=>"regular",
   "source"=>"api",
   "tags"=>[],
   "utm_campaign"=>"",
   "utm_medium"=>"",
   "utm_source"=>""
}

我是否正确发送了数组?我是否正确地发出我的 POST 请求? Buttondown 文档中是否有任何内容表明我没有正确发送标签?

【问题讨论】:

  • 您是否在标题中设置了'Content-Type' => 'application/json'?你试过打电话给body.to_json吗?

标签: ruby rest httparty


【解决方案1】:

正如spickermann 所说的评论,我需要在标题中设置内容类型并在正文中调用to_json

    headers = {
      ...
      'Content-Type' => 'application/json'
    }
    res = HTTParty.post(
      'https://api.buttondown.email/v1/subscribers', 
      body: body.to_json,
      headers: headers
    )

【讨论】:

    猜你喜欢
    • 2014-10-07
    • 2021-01-03
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    • 2021-08-13
    相关资源
    最近更新 更多