Request headers tell the server what kind of data you are sending and what kind of data you want back. This helps the server understand and respond correctly.
Request headers (Content-Type, Accept) in Rest API
Content-Type: media/type Accept: media/type
Content-Type tells the server the format of the data you are sending.
Accept tells the server the format you want in the response.
Content-Type: application/json Accept: application/json
Content-Type: application/x-www-form-urlencoded Accept: text/html
Content-Type: multipart/form-data Accept: application/json
This Python program sends JSON data to a test server with Content-Type and Accept headers set to application/json. It prints the status code, the response content type, and the JSON data the server received.
import requests url = 'https://httpbin.org/post' headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } data = {'name': 'Alice', 'age': 30} response = requests.post(url, json=data, headers=headers) print(response.status_code) print(response.headers['Content-Type']) print(response.json()['json'])
Always set Content-Type when sending data so the server knows how to read it.
Use Accept to tell the server what response format you prefer, but the server may not always honor it.
Headers are case-insensitive but usually written with capital letters and hyphens.
Content-Type header tells the server the format of your request data.
Accept header tells the server what response format you want.
Setting these headers helps APIs communicate clearly and avoid errors.