How to make a POST request with JavaScript

How to make a POST request with JavaScript

Photo by Bundo Kim

Probably the simplest way to make a POST request with JavaScript is using the Fetch API.

This should be supported in most modern browsers.

Normally, you would just pass a URL to the ​fetch ​function and it would perform a GET request.

To change this to a POST request, simply pass in an object specifying the method you want to use.

fetch​(​'https://myurl.com/'​,​ {​ method​:​ ​'POST'​ ​})
  .​then​(​result ​=>​ result​.​json​())
  ​.​then​(​console​.​log​);

You can use the returned promise from the call to fetch to extract an object from the data returned and make use of it if the endpoint your calling returns JSON data.

Watch the tutorial for more details.

Follow me on Twitter for more tutorials 👍