【问题标题】:POST to VoilaNorbert API from Rails with HTTParty使用 HTTParty 从 Rails POST 到 VoilaNorbert API
【发布时间】:2020-01-04 03:55:27
【问题描述】:

我正在尝试使用 HTTParty Rails gem 将数据发布到标准 VoilaNorbert search API endpoint

response = HTTParty.post(
  'https://api.voilanorbert.com/2018-01-08/search/name',
  query: {
    'name': 'Elon Musk', 
    'domain': 'https://www.tesla.com/'
  },
  headers: {
    'Content-Type': 'application/json',
    'Authorization': VOILANORBERT_API_TOKEN
  }
)

但是,当我尝试这个简单的请求时,我得到了响应:

 => {"name"=>["This field is required."]} 

我的理解是上面的查询块应该传递名称字段,在本例中是“Elon Musk”。

我在这里错过了什么?

【问题讨论】:

    标签: ruby-on-rails httparty


    【解决方案1】:

    经过反复试验,我成功地意识到我需要更改有效负载以在请求正文中发送表单数据,而不是作为查询参数。这需要使用 body 块对表单字段进行编码,以及请求上的 multipart 方法指示正在上传数据。

    response = HTTParty.post(
      'https://api.voilanorbert.com/2018-01-08/search/name',
       body: {
         name: 'Elon',
         domain: 'https://www.tesla.com/'  
       },
       multipart: true,
       headers: {
         'Content-Type': 'application/json',
         'Authorization': VOILANORBERT_API_TOKEN
       }
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-19
      • 2019-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多