P粉4587250402023-08-21 10:51:25
I think your problem is that jsfiddle
can only handle form-urlencoded
requests. But the correct way is to pass the correct json
as the request body:
fetch('https://httpbin.org/post', { method: 'POST', headers: { 'Accept': 'application/json, text/plain, */*', 'Content-Type': 'application/json' }, body: JSON.stringify({a: 7, str: 'Some string: &=&'}) }).then(res => res.json()) .then(res => console.log(res));
P粉8199374862023-08-21 10:15:17
Using ES2017’s async/await
support, here’s how to POST
JSON data:
(async () => { const rawResponse = await fetch('https://httpbin.org/post', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({a: 1, b: 'Textual content'}) }); const content = await rawResponse.json(); console.log(content); })();