What are GET, POST, PUT, and DELETE HTTP request and how to use them?

sam
sam
134 Points
62 Posts

What are GET, POST, PUT, and DELETE HTTP request and how to use them?

Views: 8165
Total Answered: 1
Total Marked As Answer: 0
Posted On: 14-Dec-2015 08:48

Share:   fb twitter linkedin
Answers
Stevan
Stevan
26 Points
13 Posts
     

REST API
REST (Representational State Transfer) was introduced by Roy Fielding in 2000 .in his doctoral dissertation. REST is an architectural style for designing distributed systems. REST is most commonly associated with it.

Principles of REST
Resources expose easily understood directory structure URIs.
Representations transfer JSON or XML to represent data objects and attributes.
Messages use HTTP methods explicitly (for example, GET, POST, PUT, and DELETE).
Stateless interactions store no client context on the server between requests. State dependencies limit and restrict scalability. The client holds session state.

HTTP methods
We can use HTTP methods to map CRUD (create, retrieve, update, delete) operations to HTTP requests.

GET
Retrieve information.

Retrieve an address with an ID of 1:
GET /addresses/1

POST
Request that the resource at the URI do something with the provided entity. Often POST is used to create a new entity, but it can also be used to update an entity.

Create a new address:
POST /addresses

PUT
Store an entity at a URI. PUT can create a new entity or update an existing one. A PUT request is idempotent. Idempotency is the main difference between the expectations of PUT versus a POST request.

Modify the address with an ID of 1:
PUT /addresses/1
Note: PUT replaces an existing entity. If only a subset of data elements are provided, the rest will be replaced with empty or null.

PATCH
Update only the specified fields of an entity at a URI. A PATCH request is neither safe nor idempotent (RFC 5789). That's because a PATCH operation cannot ensure the entire resource has been updated.

PATCH /addresses/1

DELETE
Request that a resource be removed; however, the resource does not have to be removed immediately. It could be an asynchronous or long-running request.

Delete an address with an ID of 1:
DELETE /addresses/1

HTTP status codes
Status codes indicate the result of the HTTP request.

1XX - informational
2XX - success
3XX - redirection
4XX - client error
5XX - server error

Media types
The Accept and Content-Type HTTP headers can be used to describe the content being sent or requested within an HTTP request. The client may set Accept to application/json if it is requesting a response in JSON. Conversely, when sending data, setting the Content-Type to application/xml tells the client that the data being sent in the request is XML.

Posted On: 21-Sep-2018 20:40
banner

Blog

Active User (2)

 Log In to Chat