GET and POST are both forms of requests that are used to send and receive data from a server, but there are differences between the two. Here is a brief overview:
GET
- Used to retrieve information
- Data is sent via query string
- Information is clearly visible in the URL!
- Limited amount of data can be sent
POST
- Used to post data to the server
- Used to write/Create/Update
- Data is sent via request body, not a query string
- Can send any source of data (JSON)
With this being outlined, we need to remember that these are NOT Hard Rules that we MUST adhere to. You could for example "send" data with a GET request, it's just generally considered Not Good Practice.
Get requests are generally used to search, or retrieve information.Their requests are sent via a query string which means that they are limited in the amount of data they can request. They are also limited to text requests. Also, the query string is visible in the browser address bar making it an unsecure request. This is why they are NOT generally used to send, update, or delete data.
POST requests are generally used to send, update, or delete data. They are included in the request body as opposed to a query string, so they will NOT be displayed in the browser address bar. Also, because the request is sent in the request body, they are not limited to any data type or how much data they can send.
In summary, GET requests are generally used to receive data while POST requests are generally used to send, update, or delete data.
Syntax
We have already been using get requests, so we've seen the basic syntax for them. Post requests are essentially the same syntax, but we we're going to break it down a little.
When we set up our localhost server we know we're going to be running express, so we do some basic server coding to import express and start the server in "listening" mode. Here's the Js code:
- JavaScript
- const express = require('express');
- const app = express();
- const port = 3000;
-
- app.listen( port, () => {
- console.log(`Server Online : Listening on port: ${ port }`);
- })
So we've required (imported) express and assigned it to the variable app which gives us access to the express methods. We then put the server in listen mode, on the designated port, waiting for a req.
Now we can set our get request:
- JavaScript
- app.get( '/', ( req, res ) => {
- res.render('root webpage')
- })
This will respond to a request of "/" at http://localhost:3000 by displaying the root webpage.
And, our post request syntax is the same, replacing get with post:
- JavaScript
- app.post( '/', ( req, res ) => {
- res.render('root webpage')
- })
The server syntax is the same for both requests, but the difference between get and post, is how the request's are processed. Also, you would not normally use post to request a webpage, like we're showing here. We're just trying to show the syntax at this point.
And remember that a post request is generally used to send, update, or delete data. Post is most often used in conjunction with forms to send and process form data. We will delve into that topic in the next discussion.