When working with React, one common task is to communicate with a server through HTTP requests. Fetch is a modern, promise-based API that makes this process straightforward. In this post, we’ll explore how to perform HTTP POST requests in React using Fetch, complete with practical examples.
Introduction to Fetch API
The Fetch API provides a simple interface for fetching resources. It’s a more powerful and flexible replacement for XMLHttpRequest`. The main features of Fetch are:
- Simple and clean API
- Supports promises
- Built-in support for handling various data formats (e.g., JSON)
- Browser compatibility (modern browsers)
Setting Up a React Project
Before diving into the code, ensure you have a React project set up. If you don’t have one yet, you can create it using create-react-app:
1
2
3
4
npx create-react-app react-fetch-post
cd react-fetch-post
npm start
Basic POST Request with Fetch
To send a POST request using Fetch, you need to specify the method, headers, and body of the request. Here’s a basic example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import React, { useState } from 'react';
const App = () => {
const [data, setData] = useState({ name: '', age: '' });
const [response, setResponse] = useState(null);
const handleChange = (e) => {
const { name, value } = e.target;
setData({
...data,
[name]: value,
});
};
const handleSubmit = async (e) => {
e.preventDefault();
try {
const res = await fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
const result = await res.json();
setResponse(result);
} catch (error) {
console.error('Error:', error);
}
};
return (
<div>
<h1>POST Request with Fetch</h1>
<form onSubmit={handleSubmit}>
<input
type="text"
name="name"
value={data.name}
onChange={handleChange}
placeholder="Name"
/>
<input
type="text"
name="age"
value={data.age}
onChange={handleChange}
placeholder="Age"
/>
<button type="submit">Submit</button>
</form>
{response && (
<div>
<h2>Response</h2>
<pre>{JSON.stringify(response, null, 2)}</pre>
</div>
)}
</div>
);
};
export default App;
Handling Responses
In the above example, after the POST request is sent, the response is handled by converting it to JSON and setting it in the component state. You can then render this response in your component.
Error Handling
Proper error handling is crucial for a robust application. Here’s how you can handle errors in the fetch request:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const handleSubmit = async (e) => {
e.preventDefault();
try {
const res = await fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
if (!res.ok) {
throw new Error('Network response was not ok');
}
const result = await res.json();
setResponse(result);
} catch (error) {
console.error('Error:', error);
setResponse({ error: 'An error occurred. Please try again.' });
}
};
Advanced POST Request Examples
Sending Form Data If you need to send form data (e.g., for file uploads), you can use the FormData API:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const handleSubmit = async (e) => {
e.preventDefault();
const formData = new FormData();
formData.append('name', data.name);
formData.append('age', data.age);
try {
const res = await fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
body: formData,
});
if (!res.ok) {
throw new Error('Network response was not ok');
}
const result = await res.json();
setResponse(result);
} catch (error) {
console.error('Error:', error);
setResponse({ error: 'An error occurred. Please try again.' });
}
};
Handling Authentication For authenticated requests, include the authorization token in the headers:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const handleSubmit = async (e) => {
e.preventDefault();
try {
const res = await fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_TOKEN_HERE',
},
body: JSON.stringify(data),
});
if (!res.ok) {
throw new Error('Network response was not ok');
}
const result = await res.json();
setResponse(result);
} catch (error) {
console.error('Error:', error);
setResponse({ error: 'An error occurred. Please try again.' });
}
};