I send JSON object using fetch() this way:
async testPost(){
const url = 'https://untitled-0clyb6aowq2u.runkit.sh';
var payload = { "test" : 1 };
const settings = {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
body: JSON.stringify(payload)
}
};
try {
const resp = await fetch(url, settings);
const data = await resp.json();
if(data.ok != 1){
console.log('FATAL ERROR PIZDEC: ');
console.log(data);
return { ok: 0, error: data};
}
console.log('Success:' + data.success);
return;
}
catch (e) {
return { ok: 0, error: e };
}
}
Server always gets the request, but there's just an empty object in the body. I've tried:
Why can the body just disappear?
I don't believe my question is a duplicate of this.
You shouldn't put body
inside headers
, see example on MDN here.