What is the difference between HTTP PUT and HTTP POST?
This shot provides a comparison between PUT vs. POST requests.
HTTP POST
-
In a RESTful API, HTTP
POSTrequests work well with HTTP requests that create data.CRUD create, read, update, delete -
HTTP
POSTsends data to the HTTP server. -
The HTTP
POSTrequests allow you to send data from a client to a server. -
The HTTP
POSTmethod requests that the enclosed entity be processed by the user agent and incorporated into the resource identified by the Request - .URI uniform resource identifier -
Below is a sample
curlcommand for the HTTPPOSTrequest.
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
POSTrequest 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
POSTrequests, the HTTP body contains the entity sent to the server. -
The HTTP
POSTis usually used when you submit a web form or any HTTP data that can’t be sent in the HTTPGETmethod. -
The HTTP
POSTis also used when uploading files to an HTTP server, e.g., uploading images to the HTTP server where the HTTPPOSTbody contains one or more files. -
HTTP
POSTrequests 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
PUTrequests work well with CRUD HTTP requests that modify data. -
HTTP
PUTsends data to a resource. -
The HTTP
PUTrequest allows you to edit existing HTTP resources. -
The HTTP
PUTmethod 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
curlcommand for the HTTPPUTrequest 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
PUTrequest 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
PUTis idempotent, which means that multiple identical HTTPPUTrequests 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
PUTshould only be performed once, expecting that the HTTP server will remember. -
HTTP
PUTrequests often want to change or update an existing HTTP resource, such as updating a purchase order to reflect new items.
Free Resources