Ruby Gem Rest-Client vs cURL,HTTP 415

时间:2016-10-14 15:03:54

标签: ruby-on-rails ruby rest curl http-status-code-415

我有一个cURL调用,但是当我使用Ruby Gem rest-client进行翻译时,我得到了:

  

RestClient :: UnsupportedMediaType:415不支持的媒体类型

以下是我使用过的cURL:

curl \
-X POST \
-H "Content-Type:application/json" \
-H "Authorization: Bearer MY_TOKEN" \
-H "Amazon-Advertising-API-Scope: MY_SCOPE" \
-d '{"campaignType":"sponsoredProducts","reportDate":"20161013","metrics":"impressions,clicks,cost"}' \
https://advertising-api.amazon.com/v1/productAds/report

这是返回HTTP 415状态的Ruby:

yesterday = Date.today - 1
RestClient::Request.execute(
  method:  :post,
  url:     'https://advertising-api.amazon.com/v1/productAds/report',
  headers:
  {
    'Content-Type'                 => 'application/json',
    'Authorization'                => "Bearer #{ENV['AD_ACCESS_TOKEN']}",
    'Amazon-Advertising-API-Scope' => ENV['AD_PROFILE_ID']
  },
  payload:
  {
    'campaignType' => 'sponsoredProducts',
    'reportDate' => "#{yesterday.year}#{yesterday.month}#{yesterday.day}",
    'metrics' => 'impressions,clicks,cost'
  }
)

1 个答案:

答案 0 :(得分:0)

有效负载哈希需要转换为JSON。

...
payload:
{
  ...
}.to_json
...