Answer the question
In order to leave comments, you need to log in
Why are parameters not passed in the POST request?
I do according to the official documentation:
https://facebook.github.io/react-native/docs/network
const response = fetch(
"<PLACE POST REQUEST URL HERE>", {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
key1: 'value1',
key2: 'value2',
}),
})
.then((response) => { return response.json() } )
.catch((error) => console.warn("fetch error:", error))
.then((response) => {
console.log(JSON.stringify(response));
})
//POST json
var dataToSend = {title: 'foo', body: 'bar', userId: 1};
//making data to send on server
var formBody = [];
for (var key in dataToSend) {
var encodedKey = encodeURIComponent(key);
var encodedValue = encodeURIComponent(dataToSend[key]);
formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");
//POST request
fetch('https://jsonplaceholder.typicode.com/posts', {
method: "POST",//Request Type
body: formBody,//post body
headers: {//Header Defination
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
})
.then((response) => response.json())
//If response is in json then in success
.then((responseJson) => {
alert(JSON.stringify(responseJson));
console.log(responseJson);
})
//If response is not in json then in error
.catch((error) => {
alert(JSON.stringify(error));
console.error(error);
});
Answer the question
In order to leave comments, you need to log in
tore this piece out of the live project
let body = { username: email, password: password, grant_type: "password" }
const searchParams = Object.keys(body)
.map(key => {
return encodeURIComponent(key) + "=" + encodeURIComponent(body[key])
})
.join("&")
await fetch(`${API_ROOT}/Token`, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Platform: Platform.OS,
buildVersion: VersionNumber.buildVersion,
},
body: searchParams,
}).then((response)=>console.log("response",response))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question