415 Unsupported Media Type Fetch POST request

625 views Asked by At

I'm attempting to send a fetch POST request to a few 3rd party APIs. When testing this call in Postman I'm able to get a successful 200 response. Using the same code from Postman in my project returns a 415 Unsupported Media Type. I've been searching for a solution and I've tried sending it with the following headers as suggested by several posts and articles.

myHeaders.append("Content-Type", "application/json");
myHeaders.append("Access-Control-Allow-Origin", "*");
myHeaders.append("Accept", "application/json");

I've attached my API call down below and would be super helpful if anyone is able to spot why I'm getting this 415.

var myHeaders = new Headers();
myHeaders.append("xxxxx_api_key", "");
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Access-Control-Allow-Origin", "*");
myHeaders.append("Accept", "application/json");

var raw = JSON.stringify({
    api_key: "xxxxx_api_key",
    profiles: [{
      email: "[email protected]",
      phone_number: "17326138213",
      example_property: "valueB",
    }],
  });
console.log(raw);
var requestOptions = {
  method: "POST",
  mode: "no-cors",
  headers: myHeaders,
  body: raw,
  redirect: "follow",
};

fetch("https://a.klaviyo.com/api/v2/list/XXXXX/members", requestOptions)
  .then((response) => response.json())
  .then((result) => console.log(result))
  .catch((error) => console.log("error", error));

This is the working request exported from Postman

curl --location --request POST 'a.klaviyo.com/api/v2/list/XXXXX/members' \ 
   --header 'Content-Type: application/json' \ 
   --header 'xxx_api_key;' \ 
   --data-raw '{ "api_key": "xxx_api_key", "profiles": [ { "email": "[email protected]", "phone_number": "17326138213", "example_property": "valueB" } ] }'
0

There are 0 answers