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.
Can anyone help with this issue?
A fetch() method can be used with many type of requests such as POST, GET, PUT and DELETE.
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.
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.
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.
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.
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))) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With