415 on Android but not on iOS

Hello,

I’ve an application for iOS and Android. The code is exactly the same for both devices.

I’m trying to make a POST request on my local API. It’s perfectly working on iOS but in Android it’s look like I cannot set the headers with fetch.

handleActions = async ({ action, id }) => {
    const result = await fetch('http://url', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        action,
        ids: [id]
      })
    });
    const json = await result.json();
    console.log(json);
  };

I’ve a 200 on iOS but a 415 on Android with this code.

Do you have a solution ?

Thanks.

Hey @nyhillius,

415 indicates an unsupported media type. What is in your payload?

Cheers,
Adam

Hey,

I know it is an unsupported media type, I did the API. The problem it is that in Android, the header “Content-Type” cannot be set. Because of this, the API return 415.

Thanks.

You could try using XHR and see if that works.

const upload = async (method, url, file) => {
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.open(method, url);
    xhr.setRequestHeader('Content-Type', file.type);
    xhr.onload = () => {
      if (xhr.status !== 200) {
        reject(
          new Error(
            `Request failed. Status: ${xhr.status}. Content: ${
              xhr.responseText
            }`,
          ),
        );
      }

      resolve(xhr.responseText);
    };
    xhr.send(file);
  });
};