Structure of a Request and Response
Understand the CRUD operations and what the structure of a request and response in a web application is.
We'll cover the following
Before moving on to the structure of a request and a response, it is important to understand CRUD operations.
What does CRUD stand for?
CRUD is an abbreviation for Create, Read, Update, and Delete. These are some of the main things that you might want to do when you are communicating with your client and server. On a REST API, the equivalent of those are actually HTTP methods or HTTPS operations.
-
What’s the equivalent of “create” in an HTTP method?
It is
POST
. APOST
request is used to send data to the server, for example, customer information, file upload, some confidential data, etc. In other words, we usePOST
when we want to submit the data to the server, which needs to be processed by the specified resource. -
What’s the equivalent of “read” in an HTTP method?
It is
GET
. TheGET
method is used to retrieve or get the information from the given server using a givenURI
. Requests usingGET
should only retrieve data and should have no other effect on the data. Any changes in the data should be done using thePOST
request only. -
What’s the equivalent of “update” in an HTTP method?
It is
PUT
. ThePUT
operation replaces or updates all the current representations of the target resource with the uploaded/sent content. -
What’s the equivalent of “delete” in an HTTP method?
It is
DELETE
. TheDELETE
operation removes or deletes all the current representations of the target resource given by aURI
.
What is the structure of a request?
There are majorly four elements in a request.
- Operation: These are the HTTP methods that we discussed above.
- Endpoint: This is your REST API endpoint.
- Parameters/body: This is some data that you might send in the request. We will see some examples in a moment.
- Headers: This is a special part of a REST API request that might contain things like an API key or some authentication data.
What is the structure of a response?
This is typically in the form of JSON data, but the REST API can also return XML, YAML, or any other format depending on what the client requests.
Let’s return to our example and look at a few different scenarios that might happen with your ice cream shop.
You want to display what is currently in stock. We want to get the flavors that are in stock. Now, what does our REST API request look like? You have a GET
as the operation because you want to get those flavors, and the endpoint is /api/flavors
. In response, you will get an array of those flavor resources. We see strawberries and mint chocolate in stock.
Get hands-on with 1400+ tech skills courses.