Simple POST request using the fetch API Last Updated : 25 May, 2020 Comments Improve Suggest changes Like Article Like Report The fetch() method, like the XMLHttpRequest and Axios request, is used to send the requests to the server. The main difference is that the Fetch API uses Promises, which enables a simpler and cleaner API. You will get the whole Get and Post method using fetch API Syntax: fetch(url, { config }) .then(res => { // Handle response }) .catch(err => { // Handle errors }) Since fetch returns a Promise, we can also use async/await keywords to make requests: async () => { const resp = await fetch('http://example.com/example.json'); // Handle response } Create a POST request using fetch(): The POST request is widely used to submit forms to the server. javascript fetch(url, { method: 'POST', headers: { "Content-type": "application/x-www-form-urlencoded; charset=UTF-8" }, credentials: 'include', body: 'foo=bar&lorem=ipsum' }) .then(res.json()) .then(res => { // Handle response console.log('Response: ', res); }) .catch(err => { // Handle error console.log('Error message: ', error); }); Explanation: To create a POST request we need to specify some parameters with the request such as method, headers, etc. First, we need to specify the request method (GET, POST, DELETE, etc.) which is POST in our case. This is followed by the Content-type, which tells the client what the content type of the returned data actually is. The credentials key is optional and should be used if you want to make a fetch request with credentials such as cookies. Then we set the body which consists of the data we wish to pass to the server. The response of a fetch() request is a stream object, which means that when we call the json() method, a Promise is returned since the reading of the stream will happen asynchronously. If the API returns a status of 201 (the HTTP status code for Created), the data returned by the API can be accessed- res(response). In case an error occurs, the code inside the catch block will be executed. Comment More infoAdvertise with us Next Article Simple POST request using the fetch API V verma_anushka Follow Improve Article Tags : JavaScript Similar Reads Sending a Post request in postman pre-script Postman stands out as a go-to tool for crafting and testing APIs, offering a robust set of features including API monitoring and script-based testing. Pre-request scripts which are effectively snippets of JavaScript code, play an important role in API testing. This article focuses on discussing send 3 min read Using Curl to make REST API requests REST APIs are essential for modern web applications, enabling programmatic interaction with data and functionality. Curl is a command-line tool for making web requests, often used directly from the terminal. For example, curl -L ip.ba3a.tech fetches IP address details in JSON format, just like visit 5 min read Get and Post method using Fetch API The fetch() method is used to send HTTP requests to the server without refreshing the page, providing an efficient alternative to the older XMLHttpRequest object. One of the main advantages of fetch() is that it uses Promises, which allow for better management of asynchronous operations compared to 4 min read How to make simple PUT request using fetch API by making custom HTTP library ? The fetch() method is used to send the requests to the server without refreshing the page. It is an alternative to the XMLHttpRequest object. It will be taking a fake API that will contain Array as an example and from that API we will show to PUT/Update  data by  fetch API  method by making custom H 2 min read How to Retrieve the Request Object in PostMan Postman is a popular API testing tool used by developers to test, document, and share APIs. While Postman primarily focuses on sending HTTP requests and receiving responses, it also provides features for inspecting request objects. In this article, we'll explore how to retrieve the request object in 5 min read Like