mvdb mvdb - 3 months ago 12
iOS Question

fetch() doing GET instead of POST on react-native (iOS)

I have the following code in my component:

fetch('https://domain.com/api', {
method: 'POST',
headers: {'Accept': 'application/json', 'Content-Type': 'application/json'},
body: JSON.stringify({
key: 'value'
})
}).
then((response) => {
console.log('Done', response);
});


And every time the request is a GET (checked server logs). I thought it was something to do with
CORS
(but apparently no such thing in
react-native
) and ATS (but already turned off by default, plus my domain is HTTPS). I've tried from a browser and from a
curl
and it worked perfectly, so a priori no issue on server configuration. Any idea what's going on here?

I'm using the latest
react-native
version.

Answer

After further digging, it was definitely an issue with the API + fetch. I was missing a slash at the end of the URL and the API issued a 301, that fetch didn't handle correctly. So I don't know if there is something to fix in the fetch function (and underlying mechanisms) but this fixed my issue :)

Comments