POST REQUEST
For effective interaction between clients (application) and servers (computer), there must be an efficient transfer of data. This is where HTTP comes in handy. It is designed in a way that allows information to be sent in a format that can be understood by both the client and the server. HTTP works as a request-response protocol between a browser/ application and a computer that hosts a website. Calling or submitting various HTTP requests can be done using multiple methods. The POST request is one of them.
What is POST Request Method?
POST is the HTTP method that is designed to send loads of data to a server from a specified resource. Most common HTML forms on the web operate using this request method. It usually transmits relatively small loads of data to a receiver. This method allows data to be sent as a package in a separate communication with the processing script. This means that data sent through the POST method will not be visible in the URL, as parameters are not sent along with the URI.
The format of an HTTP POST should have HTTP headers, followed by a blank line, followed by the request body. POST request can be used to submit a web form or upload a file, but it is critical to ensure that the receiving application resonates with the format being used. The Content-Type header indicates the type of body in the POST request.
POST Examples
For example, if you want to pass the username and password using the API URL, https://api.example.com/v2/login, which is used to authenticate the application.
Curl -X POST -H "Content-Type: application/json" -d '{"username":"abc","password":"abc"}' https://api.example.com/v2/login
You can also note down the username and password in a user JSON file
Curl -X POST -H "Content-Type: application/json" -d @user.json https://api.example.com/v2/login
POST vs GET
Although POST and GET are the most commonly used HTTP request methods, they have many differences.
While the HTTP POST method is used to send data to a server to create or update a resource, the HTTP GET method is used to request data from a specified resource and should have no other effect.
HTTP POST request provides additional data from the client to the server message body. On the other hand, the GET request incorporates all the required data in the URL. The method specified when information is transferred can determine how form data is submitted to the server. When it comes to POST, form data is visible within the message body of the HTTP request. On the other hand, if the method is GET, all form data is encoded into the URL and appended to the action URL as query string parameters.
POST request parameters can’t be saved in browser history, can’t be bookmarked, has no restriction on form data length, cannot be cached, and is more hack-proof. On the other hand, GET request parameters can be archived in the browser history, can be bookmarked, it is easier to hack, has restriction on form data length, and can be cached.
POST vs PUT
While both POST and PUT HTTP request methods seem to be sending data to a server inside the body, what makes them different? While the POST method is non-idempotent and its response can be cached, the PUT method is idempotent, and its responses are not cacheable.