post request react using fetch code example
Example 1: react post request
componentDidMount() {
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title: 'React POST Request Example' })
};
fetch('https://jsonplaceholder.typicode.com/posts', requestOptions)
.then(response => response.json())
.then(data => this.setState({ postId: data.id }));
}
Example 2: how to post data using fethch
fetch('url here', {
method: 'POST',
headers: {'Content-Type':'application/x-www-form-urlencoded'},
body: 'foo=bar&blah=1'
});