title: R - API request for Euromonitor
body: <p>Given an API specification of ...</p>
  
  <pre><code>POST https://api.euromonitor.com/authentication/connect/token HTTP/1.1
  
  Host: api.euromonitor.com
  
  Content-Type: application/x-www-form-urlencoded
  
  Ocp-Apim-Subscription-Key: xxxyyyzzz
  
  grant_type=string&amp;username=xxx@acme.com&amp;password=yyyzzz
  </code></pre>
  
  <p>... how do I incorporate the subscription-key and grant_type parameters into an R program to request the data?</p>
  
  <p>I am assuming I send a <code>Post</code> request but everything I have tried so far (such as the below) leads to a 400 Bad Request error.</p>
  
  <pre><code>url &lt;- "https://api.euromonitor.com/authentication/connect/token HTTP/1.1"
  
  body_list &lt;- list(subs_key="xxxyyyzzz", 
                    grant_type="string&amp;username=xxx@acme.com&amp;password=yyyzzz")
  
  r &lt;- POST(url, body=body_list, encode="form", verbose())
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Closer reading of the Euromonitor documentation reveals grant_type is in the body and the rest of the parameters are in the header. I have altered my program as such:</p>
  
  <pre><code>url &lt;- "https://api.euromonitor.com/authentication/connect/token HTTP/1.1"
  r &lt;- POST(url, add_headers(.headers=c("Host"="api.euromonitor.com",
                             "Content-Type"="application/x-www-form-urlencoded",
                             "Ocp-Apim-Subscription-Key"="xxxyyyzzz")),
        body="grant_type=password&amp;username=xxx@acme.com&amp;password=yyyzzz", verbose())
  </code></pre>
  
  <p>But I am still getting a 400 Bad Request error:</p>
  
  <pre><code>-&gt; POST /authentication/connect/token HTTP/1.1 HTTP/1.1
  -&gt; Host: api.euromonitor.com
  -&gt; User-Agent: libcurl/7.59.0 r-curl/3.2 httr/1.4.0
  -&gt; Accept-Encoding: gzip, deflate
  -&gt; Accept: application/json, text/xml, application/xml, */*
  -&gt; Content-Type: application/x-www-form-urlencoded
  -&gt; Ocp-Apim-Subscription-Key: xxxyyyzzz
  -&gt; Content-Length: 69
  -&gt; 
  &gt;&gt; grant_type=password&amp;username=xxx@acme.com&amp;password=yyyzzz
  
  &lt;- HTTP/1.1 400 Bad Request
  &lt;- Content-Type: text/html; charset=us-ascii
  &lt;- Date: Tue, 12 Mar 2019 02:55:39 GMT
  &lt;- Connection: close
  &lt;- Content-Length: 311
  </code></pre>