Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - Fetch POST request is sending as GET request

Tags:

I'm having issues when using FETCH.

I am trying to make a POST request using FETCH in react-native.

    fetch("http://www.example.co.uk/login", {         method: 'POST',         headers: {             'Accept': 'application/json',             'Content-Type': 'application/json'         },         body: JSON.stringify({             username: 'test',             password: 'test123',         })     })          .then((response) => response.json())         .then((responseData) => {             console.log(                 "POST Response",                 "Response Body -> " + JSON.stringify(responseData)             )         })         .done(); } 

When I inspect this call using Charles it is recorded as a GET request and the username and password that should be in the body are not there.

enter image description here

Can anyone help with this issue?

like image 867
samb90 Avatar asked Feb 01 '16 21:02

samb90


People also ask

Does fetch use Get or POST?

A fetch() method can be used with many type of requests such as POST, GET, PUT and DELETE.

How do I send a POST request with fetch React?

React Fetch POST examplestringify() on the object before passing it in the body of the request and set: "post" for method. "application/json" for the header Content-Type.

How do I send a POST request in React Native?

To trigger a Post request from the UI side in react -native, we can send the Request option as a second Parameter. Project Structure: The project should look like this: Example: Here, we are sending request options as a second parameter along with the body. This body is handled in API.

Does fetch work with React Native?

React Native provides the Fetch API for your networking needs. Fetch will seem familiar if you have used XMLHttpRequest or other networking APIs before. You may refer to MDN's guide on Using Fetch for additional information.


2 Answers

I had this issue when the POST request was to an HTTPS (rather than HTTP) server. For some reason, it would somewhere along the way be converted into a GET request.

It turns out what I was doing incorrectly was sending the request to http://myserver.com:80 rather than to https://myserver.com:443. Once I switched it over to the proper prefix and ports, the request would properly send as a POST.

like image 34
Josh Baker Avatar answered Sep 18 '22 14:09

Josh Baker


This worked for me:

let data = {   method: 'POST',   credentials: 'same-origin',   mode: 'same-origin',   body: JSON.stringify({     appoid: appo_id   }),   headers: {     'Accept':       'application/json',     'Content-Type': 'application/json',     'X-CSRFToken':  cookie.load('csrftoken')   } } return fetch('/appointments/get_appos', data)         .then(response => response.json())  // promise         .then(json => dispatch(receiveAppos(json))) }  
like image 57
aarkerio Avatar answered Sep 16 '22 14:09

aarkerio