1

我有一个有效的 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'
  }
)
4

1 回答 1

1

负载哈希需要转换为 JSON。

...
payload:
{
  ...
}.to_json
...
于 2016-10-14T15:25:45.640 回答