GET and POST are two different types of HTTP request methods. HTTP protocol supports many methods to transfer data from the server or perform any operation on the server. The HTTP protocol supports the methods, e.g. GET, POST, PUT, DELETE, PATCH, COPY, HEAD, OPTIONS, etc. Before we dive into the main difference between GET and POST request methods, let us look at what does these HTTP methods.
What is GET request method?
Data is being requested from a specific resource (through some API URL). Here in the example, a dummy API is used to demonstrate, how GET request actually works.
What is POST request method?
Data is sent to be processed to a specific resource (through some API URL). Here in the example, a dummy API is used to demonstrate, how POST request actually works.
Example: The example demonstrates GET request method.
// Instantiating new XMLHttpRequest() method let xhr = new XMLHttpRequest(); // open method to pass request // type, url and async true/false xhr.open('GET', // onload function to get data xhr.onload = function () { if (this.status === 200) { console.log(JSON.parse(this.responseText)); } } // Send function to send data xhr.send() |
Example: The example demonstrates POST request method.
// Instantiating new XMLHttpRequest() let xhr = new XMLHttpRequest(); // open method to pass request // type, url and async true/false xhr.open('POST', // Setting content-type xhr.getResponseHeader('Content-type', 'application/json'); // Perform the following when the response is ready xhr.onload = function () { if(this.status === 200){ console.log(this.responseText) } else { console.log("Some error occured") } } // Send the request as an object obj obj = `{"name":"Selmon Bhoi", "salary":"$10, 000", "age":"55"}`; xhr.send(obj); |
Difference between GET and POST:
| GET REQUEST | POST REQUEST |
| GET retrieves a representation of the specified resource. | POST is for writing data, to be processed to the identified resource. |
| It typically has relevant information in the URL of the request. | It typically has relevant information in the body of the request. |
| It is limited by the maximum length of the URL supported by the browser and web server. | It does not have such limits. |
| It is the default HTTP method. | In this we need to specify the method as POST to send a request with the POST method. |
| You can bookmark GET request. | You cannot bookmark POST request. |
| It is less secure because data sent is part of the URL | It is a little safer because the parameters are not stored in browser history or in web server logs. |
| It is cacheable. | It is not cacheable. |
| Ex. GET the page showing a particular question. | Ex. Send a POST request by clicking the “Add to cart” button. |
Recommended Posts:
- Difference between PUT and DELETE request in Vanilla JavaScript
- Simple GET and POST request using Fetch API method by making custom HTTP library
- HTTP Request vs HapiJS Request in Node.js
- Simple POST request using the fetch API
- POST request using AJAX by making Custom HTTP library
- How HTTP POST request work in node.js ?
- How to find the width of a div using vanilla JavaScript?
- When to use Vanilla JavaScript vs jQuery ?
- Creating Responsive Grid Vanilla using HTML,CSS and Bootstrap
- Creating Navbar | Vanilla HTML CSS Vs Bootstrap
- How to send a GET request from PHP?
- Express.js | app.get() Request Function
- Get request using AJAX by making Custom HTTP library
- How to make JavaScript wait for a API request to return?
- HTTP GET and POST Methods in PHP
- Two most misunderstood terms GET and POST in web development
- Get and Post method using Fetch API
- How to get form data using POST method in PHP ?
- Post/Redirect/Get (PRG) Design Pattern
- HTTP headers | Access-Control-Request-Headers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


