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
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>
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 CodesFrom 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
- Overview of REST
- REST API - HTTP Methods // Popular
- REST API - HTTP Status Codes // Popular
- Advantages of REST
- REST API - REST Architectural Constraints // Popular
- REST API - REST Architectural Properties
- REST API - REST Architectural Elements
- Difference Between SOAP vs REST Web Services
- How to Identify REST Resources
- How to Design URL to REST Resource // Popular
- How to Assign HTTP methods to REST Resources
- How to Model JSON Representation Format // Popular
- What HTTP Status Code to Return
- Restful API Design Best Practices // Popular
- Jersey Rest Hello World Example
- Jersey JAX-RS Restful CRUD Web Services Example // Popular
- Jersey Rest Developer Guide
- RESTEasy Hello World Example Tutorial // Popular
- RESTEasy JAX-RS Get, POST, PUT and DELETE Tutorial // Popular
- RESTEasy Client for GET, POST, PUT, and DELETE RESTFul APIs
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
Post a Comment