What is Payload in REST API?

In this short article, we will learn what is payload in REST API.

What is Payload in REST API?

In simple words, the payload means body in the HTTP request and response message. It's optional and depends on the HTTP method name i.e.,

 -In the case of GET HTTP method, the HTTP request message without a body.

 -In the case of the POST HTTP method, the HTTP request message with body

 The 'Content-Type' header name in the HTTP request message is used to represent payload format in the HTTP request message. For example: JSON, XML etc.

JSON payload format example:

{
    "cid": 1,
    "cname": "Ramesh",
    "email": "ramesh@gmail.com"
}

XML payload format example:

   <customer>
      <cid>1</cid>
      <cname>Ramesh</cname>
      <email>ramesh@gmail.com</email>
   </customer>

The 'Accept' header name in an HTTP request message is used to represent the expected payload format in an HTTP response message. For example: For example: JSON, XML, plain text, HTML etc.

HTTP Request Message

It is the responsibility of consumer means client application to prepare and send HTTP request message as given below:
POST /SpringBootREST/customers HTTP/1.1
Accept: application/xml
Content-Type: application/xml
Content-Length: 196
User-Agent: Java/1.7.0_25
Host: 127.0.0.1:7000
Connection: keep-alive

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
   <customer>    
      <cid>0</cid>    
      <cname>Ramesh</cname>    
      <email>ramesh@gmail.com</email>    
 </customer>
The HTTP request message always starts with HTTP method name such as GET, POST, PUT, DELETE, HEAD, etc
It is the responsibility of the consumer to prepare and send HTTP request message with the appropriate HTTP method name depending on operation:
-The GET method is used to perform the READ operation.
-The POST method is used to perform INSERT operation.
-The PUT method is used to perform UPDATE operation. 
-The DELETE method is used to perform the DELETE operation. 

From the above HTTP request, the payload means body in the HTTP request message. It's optional and depends on the HTTP method name i.e.,

 -In the case of GET HTTP method, the HTTP request message without a body.

 -In the case of the POST HTTP method, the HTTP request message with body

 The 'Content-Type' header name in the HTTP request message is used to represent payload format in the HTTP request message. For example: JSON, XML etc.

The 'Accept' header name in an HTTP request message is used to represent the expected payload format in an HTTP response message. For example: For example: JSON, XML, plain text, HTML etc.

Http Response Message

It is the responsibility of the business component (developed by the service provider) to prepare and send HTTP response message as given below:

HTTP/1.1 200 OK
Content-Type: application/xml
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
Date: Mon, 10 Nov 2019 09:45:34 GMT
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
   <customer>
      <cid>1</cid>
      <cname>Ramesh</cname>
      <email>ramesh@gmail.com</email>
   </customer>

The HTTP response message contains any one of the HTTP status code ranges between 100 and 599:

Informational   : 1xx

Successfull      : 2xx

Redirectional     : 3xx

Client side error : 4xx

Server side error : 5xx

Read more about HTTP status codes at REST API - HTTP Status Codes
From the above Response message, the payload means body in HTTP response message is optional and depends on http status code i.e.,

-In the case of the 204 status code, the HTTP response message without a body

-In the case of the 200 status code, the HTTP response message with body

The 'Content-Type' header name in the HTTP response message is used to represent payload format in an HTTP response message.

REST API Related Articles

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments