title: Implement curl post request in JavaScript Fetch API
body: <p>I'm trying to implement this post request using curl in the JS Fetch API:</p>
  <pre><code>curl --user apikey:{my_secret_apikey} --request POST --header &quot;Content-Type: application/json&quot; --data &quot;{\&quot;text\&quot;:[\&quot;Hello\&quot;],\&quot;model_id\&quot;:\&quot;en-es\&quot;}&quot; &quot;{my_secret_url}/v3/translate?version=2018-05-01&quot;
  </code></pre>
  <p>I'm having trouble implementing the API key.</p>
  <p>I tried this, but it doesn't work. I get a 401 unauthorized error back from the server.</p>
  <pre class="lang-js prettyprint-override"><code>fetch(url, {
      method: &quot;POST&quot;,
      headers: { 'Content-Type': 'application/json' },
      user: {
          &quot;apikey&quot;: blablabla_api_key
      }
      body: {
          &quot;text&quot;: [term],
          &quot;model_id&quot;: &quot;en-hi&quot;
      }
  }).then(res ........
  </code></pre>
  <p>Any help is appreciated!</p>
  <p>edit: if you have any other suggestion as to how to implement this post request into JS using some other HTTP library, that helpful too!</p>
  <p>Edited code with auth header:</p>
  <pre class="lang-js prettyprint-override"><code>let headers = new Headers();
  
  headers.append('Authorization', 'Basic ' + btoa(&quot;apikey&quot; + &quot;:&quot; + &quot;my_api_key&quot;));
  headers.append('Content-Type', 'application/json');
  fetch(url, {
      method: &quot;POST&quot;,
      headers: headers,
      body: {
          &quot;text&quot;: [&quot;Hello&quot;],
          &quot;model_id&quot;: &quot;en-es&quot;
      }
  }).then(result =&gt; {
      console.log(result);
      resolve(result.translations[0].translation);
  }).catch(err =&gt; console.log(err));
  </code></pre>
  <p>This results in a 400 Bad Request error, even though the curl request works fine.</p>