The request body holds the data you send to a server when you want to create or update something. It helps the server understand what you want to do.
0
0
Request body structure in Rest API
Introduction
When submitting a form with user information like name and email.
When uploading a file or image to a website.
When sending data to create a new record in a database.
When updating details of an existing item on a server.
When sending complex data like JSON to an API.
Syntax
Rest API
POST /api/resource HTTP/1.1 Host: example.com Content-Type: application/json { "key1": "value1", "key2": "value2" }
The request body comes after the headers, separated by a blank line.
Content-Type header tells the server the format of the data sent (like JSON).
Examples
A simple JSON body with username and password fields.
Rest API
{
"username": "johndoe",
"password": "12345"
}JSON body with different data types: string, number, and boolean.
Rest API
{
"product": "Book",
"price": 9.99,
"inStock": true
}HTML form sends data as request body in a POST request (usually as form data).
Rest API
<form action="/submit" method="POST"> <input name="name" value="Alice" /> <input name="age" value="30" /> </form>
Sample Program
This Python program sends a POST request with a JSON body to a test API. It prints the status code and the response data from the server.
Rest API
import requests url = "https://jsonplaceholder.typicode.com/posts" # Data to send in the request body body = { "title": "foo", "body": "bar", "userId": 1 } # Send POST request with JSON body response = requests.post(url, json=body) # Print response status and returned data print(f"Status code: {response.status_code}") print("Response body:") print(response.json())
OutputSuccess
Important Notes
Always set the correct Content-Type header to match your request body format.
Request body is usually used with POST, PUT, or PATCH HTTP methods.
GET requests typically do not have a request body.
Summary
The request body carries data sent to the server in POST or PUT requests.
It must be formatted correctly, often as JSON or form data.
Setting the right Content-Type header helps the server understand your data.