1(async () => {
2 const rawResponse = await fetch('https://httpbin.org/post', {
3 method: 'POST',
4 headers: {
5 'Accept': 'application/json',
6 'Content-Type': 'application/json'
7 },
8 body: JSON.stringify({a: 1, b: 'Textual content'})
9 });
10 const content = await rawResponse.json();
11
12 console.log(content);
13})();
1fetch('https://example.com/profile', {
2 method: 'POST',
3 headers: { 'Content-Type': 'application/json' },
4 body: JSON.stringify({
5 'foo': 'bar'
6 }),
7})
8 .then((res) => res.json())
9 .then((data) => {
10 // Do some stuff ...
11 })
12 .catch((err) => console.log(err));
1componentDidMount() {
2 // Simple POST request with a JSON body using fetch
3 const requestOptions = {
4 method: 'POST',
5 headers: { 'Content-Type': 'application/json' },
6 body: JSON.stringify({ title: 'React POST Request Example' })
7 };
8 fetch('https://jsonplaceholder.typicode.com/posts', requestOptions)
9 .then(response => response.json())
10 .then(data => this.setState({ postId: data.id }));
11}
1async function postData(url = '', data = {}) {
2 // Default options are marked with *
3 const response = await fetch(url, {
4 method: 'POST', // *GET, POST, PUT, DELETE, etc.
5 mode: 'cors', // no-cors, *cors, same-origin
6 cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
7 credentials: 'same-origin', // include, *same-origin, omit
8 headers: {
9 'Content-Type': 'application/json'
10 // 'Content-Type': 'application/x-www-form-urlencoded',
11 },
12 redirect: 'follow', // manual, *follow, error
13 referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
14 body: JSON.stringify(data) // body data type must match "Content-Type" header
15 });
16 return response.json(); // parses JSON response into native JavaScript objects
17}
18
19postData('', )
20 .then(data => {
21 console.log(data); // JSON data parsed by `data.json()` call
22 });
1// Example POST method implementation:
2async function postData(url = '', data = {}) {
3 // Default options are marked with *
4 const response = await fetch(url, {
5 method: 'POST', // *GET, POST, PUT, DELETE, etc.
6 mode: 'cors', // no-cors, *cors, same-origin
7 cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
8 credentials: 'same-origin', // include, *same-origin, omit
9 headers: {
10 'Content-Type': 'application/json'
11 // 'Content-Type': 'application/x-www-form-urlencoded',
12 },
13 redirect: 'follow', // manual, *follow, error
14 referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
15 body: JSON.stringify(data) // body data type must match "Content-Type" header
16 });
17 return response.json(); // parses JSON response into native JavaScript objects
18}
19
20postData('https://example.com/answer', { answer: 42 })
21 .then(data => {
22 console.log(data); // JSON data parsed by `data.json()` call
23 });
24