What is the difference between HTTP PUT and HTTP POST?

This shot provides a comparison between HTTPHyperText Transfer Protocol PUT vs. POST requests.

HTTP POST

  • In a RESTful API, HTTP POST requests work well with CRUDcreate, read, update, delete HTTP requests that create data.

  • HTTP POST sends data to the HTTP server.

  • The HTTP POST requests allow you to send data from a client to a server.

  • The HTTP POST method requests that the enclosed entity be processed by the user agent and incorporated into the resource identified by the Request - URIuniform resource identifier.

  • Below is a sample curl command for the HTTP POST request.


curl -v -X POST https://api.example.com/orders
 
> -H "Content-Type: application/json" 
--data 
'{ "shipping_address": 
    { "street_address": "1234 Sesame St", 
       "city": "Boulder", 
       "country": "US", 
       "postal_code": "80301"
    }
 }' 

  • You will find the sample responses for the HTTP POST request below.

Response(s): 
HTTP/1.1 201 Created 
HTTP/1.0 200 OK 
{ "id" : 1, "shipping_address" : {"street_address":"1234 Sesame St","city":"Boulder","country":"US", "postal_code":"80301"}, ... }

  • In HTTP POST requests, the HTTP body contains the entity sent to the server.

  • The HTTP POST is usually used when you submit a web form or any HTTP data that can’t be sent in the HTTP GET method.

  • The HTTP POST is also used when uploading files to an HTTP server, e.g., uploading images to the HTTP server where the HTTP POST body contains one or more files.

  • HTTP POST requests often request one-time data processing, such as by submitting a new review for an existing product on an eCommerce site.

HTTP PUT

  • In a RESTful API, HTTP PUT requests work well with CRUD HTTP requests that modify data.

  • HTTP PUT sends data to a resource.

  • The HTTP PUT request allows you to edit existing HTTP resources.

  • The HTTP PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, it is modified. Otherwise, the resource is created.

  • You will find a sample curl command for the HTTP PUT request below.


curl -v -X PUT https://api.example.com/orders 
> -H "Content-Type: application/json"
--data 
'{ "shipping_address": 
   { "street_address": "1234 Sesame St", 
     "city": "Boulder", 
     "country": "US", 
     "postal_code": "80301"
   }
 }' 

  • You will find the sample responses for the HTTP PUT request below.

Response(s): 
HTTP/1.1 200 OK 
HTTP/1.1 412 Precondition Failed

  • In HTTP POST requests, the HTTP body contains the updated entity that needs to be modified.

  • The HTTP PUT is idempotent, which means that multiple identical HTTP PUT requests are safe and have the same result even if there’s some error along the way (e.g., internet goes down, HTTP server crashes, etc.).

  • The HTTP PUT should only be performed once, expecting that the HTTP server will remember.

  • HTTP PUT requests often want to change or update an existing HTTP resource, such as updating a purchase order to reflect new items.

Copyright ©2024 Educative, Inc. All rights reserved