HTTP POST vs. PUT requests

It is important to note that both HTTP POST and HTTP PUT are types of HTTP request methods.

What is the HTTP request method?

The internet offers a wide variety of resources hosted on different servers. In order to access these resources, your browser must be able to send a request to the servers and display the resources for you. HTTP stands for Hypertext Transfer Protocol and is the basic format used to structure requests and responses for efficient communication between the client and server.

The client sends an HTTP request to the server, and after the message is internalized, the server sends a response. The response contains information about the status of the request.

Types of HTTP request methods

There are different HTTP request methods, but each one has a specific purpose. The main HTTP methods are as follows:

  • POST
  • GET
  • PUT
  • PATCH

For the sake of this shot, we will only discuss the POST and PUT methods.

What is the POST Method?

POST is an HTTP method designed to send loads of data between the client (application) and server (computer). The POST request method inherently requires that a web server accepts the data contained in the body of the request message, most likely to store it.

POST is commonly used when uploading a file or submitting a complete web form. A POST request is usually sent through an HTML form and results in a change on the server. HTMLHypertext Markup Language is the code used to structure a website and its content.

For POST requests, the values ​​are sent in the “body” of the request. For web forms, they will most likely be submitted with the application /x-www-form-urlencoded, multipart / form-data, or application/json media type.

POST Request in HTTP

Format of an HTTP POST request

HTTP POST format must contain HTTP headers followed by an empty string, followed by the request body. POST variables are stored in the body as key-value pairs. With the help of tools like Fiddler, you can view the raw HTTP request and the response payloads being sent. The raw content of an HTTP POST is shown below.

POST /path/script.cgi HTTP/1.1
From: django@program.com
User-Agent: HTTPTool/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 32
home=code&run+flavor=flies

What is the PUT method?

The HTTP PUT request method creates a new resource or replaces the target resource representation with the request payload.

The HTTP PUT request method puts a file or resource at the specified URLUniform Resource Locator. If a file or resource already exists at this URL, HTTP PUT replaces that file or resource. If the file or resource is not there, HTTP PUT creates it.

Overview of a PUT operation

Request

If the PUT request successfully creates a new resource, the server notifies the user by sending a 201 (Created) response, e.g., HTTP/1.1 201 Created Content-Location: /new.html.

If the target resource has the current representation and that representation has been successfully changed according to the state of the enclosed representation, the origin server sends a 200 (OK) or 204 (No content) response to indicate that the request was fulfilled, e.g., HTTP/1.1 204 No Content Content-Location: /existing.html.

HTTP PUT is paradoxically idempotent. HTTP PUT responses are not cacheable. Below is an example of raw content of an HTTP PUT request:

PUT /new.html HTTP/1.1
Host: example.com
Content-type: text/html
Content-length: 16
<p>New File</p>

Key differences between HTTP POST and HTTP PUT

Differences between HTTP PUT and HTTP POST

Free Resources