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})();
1//Obj of data to send in future like a dummyDb
2const data = { username: 'example' };
3
4//POST request with body equal on data in JSON format
5fetch('https://example.com/profile', {
6 method: 'POST',
7 headers: {
8 'Content-Type': 'application/json',
9 },
10 body: JSON.stringify(data),
11})
12.then((response) => response.json())
13//Then with the data from the response in JSON...
14.then((data) => {
15 console.log('Success:', data);
16})
17//Then with the error genereted...
18.catch((error) => {
19 console.error('Error:', error);
20});
21
22// Yeah
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));
1fetch('https://jsonplaceholder.typicode.com/posts', {
2 method: 'POST',
3 headers: {
4 'Content-Type': 'application/json',
5 },
6 body: JSON.stringify({
7 // your expected POST request payload goes here
8 title: "My post title",
9 body: "My post content."
10 })
11})
12 .then(res => res.json())
13 .then(data => {
14 // enter you logic when the fetch is successful
15 console.log(data)
16 })
17 .catch(error => {
18 // enter your logic for when there is an error (ex. error toast)
19 console.log(error)
20 })
21
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}
1var myHeaders = new Headers();
2
3var myInit = { method: 'POST',
4 headers: myHeaders,
5 mode: 'cors',
6 cache: 'default' };
7
8fetch('flowers.jpg',myInit)
9.then(function(response) {
10 return response.blob();
11})
12.then(function(myBlob) {
13 var objectURL = URL.createObjectURL(myBlob);
14 myImage.src = objectURL;
15});
16
17