Harth Harth - 1 month ago 19
reST (reStructuredText) Question

Ruby Gem Rest-Client vs cURL, HTTP 415

I have a cURL call that works but when I translate it using the Ruby Gem rest-client I get:


RestClient::UnsupportedMediaType: 415 Unsupported Media Type


Here is the cURL I used that worked:

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


Here is the Ruby that returns the HTTP 415 status:

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'
}
)

Answer

The payload hash needed to be converted to JSON.

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