6

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.

3
  • hmm, have you done some tests using GET instead of POST? maybe the code in the phone is not updated yet with 'post' Commented Aug 11, 2016 at 16:12
  • for me it works with 'post' , in lowercase Commented Aug 11, 2016 at 16:22
  • GET works of course but it's a creation of resource, so POST is what I need. And I tried lowercase too, nothing works. I didn't mention it but I'm experiencing this issue on the simulator Commented Aug 12, 2016 at 1:57

3 Answers 3

8

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 :)

Sign up to request clarification or add additional context in comments.

1 Comment

This was happening to me as well, however what was really weird is that it was only happening in a "release" build of my app deployed to a physical phone. Debug builds would POST correctly every time. Adding a trailing "/" to the route fixed the issue for me. Thanks for the help!
6

When a POST is redirected (in my case from http to https) it gets transformed into a GET. (Don't know why...)

Comments

0

Happened to me as well with React Router + Hono - as the other comments mentioned, this will be a weird redirection caused by a Cloudflare redirecting HTTP to HTTPS requests.

This was caused due to my deploy environment running in a local network in HTTP. When requesting my own API on the application-level, my application would use HTTP, which then was redirected via 302 to the HTTPS protocol but lost its method (per specification) and defaults to GET. Forcing a HTTPS there fixed the problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.