Sending JSON bodies and headers
40 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)A login or create request sends data, and the server must know what that data is and who sent it. With the wrong Content-Type or token, the same request gets 400, 401 or 415 instead of working.
Raw JSON body and the Content-Type header Postman adds
A raw JSON body is JSON text you type into the Body tab after choosing raw and then JSON. Postman then adds the header Content-Type: application/json, which tells the server how to read the bytes.
Change the method to POST, open Body, choose raw, pick JSON in the dropdown and type the JSON. The picture shows what goes on the wire.
Example 1 · POST a raw JSON body to log in
POST http://localhost:8080/api/v1/auth/loginBody tab: raw, JSON{ "email": "admin@shoplite.test", "password": "admin123"}Status: 200 OK
{
"accessToken": "sl_at_admin_7Kq2Vx9P",
"tokenType": "Bearer",
"expiresIn": 3600
}What this code does
- Choosing JSON in the Body dropdown made Postman add
Content-Type: application/json, so the server read the email and password. - The answer is an access token: a string that proves you logged in. The next requests send it in the
Authorizationheader. These are test accounts of the practice API; never paste a real password into a shared collection.
The dropdown at the end of the row says Text at first, so you must change it. Postman colours the JSON and underlines mistakes such as a missing quote before you send. Without the Content-Type header, a careful API refuses the body even when the JSON is perfect.
The Body tab also offers form-data and x-www-form-urlencoded, which HTML forms and file uploads use. A JSON API does not understand those types, so for REST APIs that take JSON, always use raw → JSON.
Custom headers and the hidden auto-generated ones
Hidden headers are the headers Postman adds on its own, such as Host, User-Agent and Postman-Token. Click the small hidden link above the Headers table to see them.
The Headers tab holds headers you add yourself, such as Accept or X-Request-Id. Postman adds more behind the scenes; the example shows the real list.
Example 2 · The headers Postman really sends
POST http://localhost:8080/api/v1/echoHeaders tab (yours) X-Request-Id fest-2026-001Body tab: raw, JSON (the same login JSON as Example 1)Status: 200 OK Request line and headers the server received: POST /api/v1/echo X-Request-Id: fest-2026-001 Content-Type: application/json User-Agent: PostmanRuntime/7.39.1 Accept: */* Cache-Control: no-cache Postman-Token: 856836d6-21e3-4dc7-9530-949fa58814a8 Host: localhost:8080 Accept-Encoding: gzip, deflate, br Connection: keep-alive Content-Length: 66
What this code does
- The practice API's
/echoendpoint sends back the request it received. OnlyX-Request-Idcame from the Headers table; the other ten headers were added by Postman. Content-TypeandContent-Lengthappear because there is a body.Postman-Tokenchanges on every send andUser-Agentnames the Postman version, so your values differ.
The hidden ones are Postman-Token, Host, User-Agent, Accept, Accept-Encoding and Connection, plus Content-Type and Content-Length when there is a body. User-Agent: PostmanRuntime/... is how a server tells Postman from a browser, and Postman-Token is a random id added to every send. A header you type yourself wins over the hidden one with the same name.
If a request works in Postman but not from your own code, compare the hidden headers with what your code sends: usually a missing Content-Type or Authorization header is the difference.
content-type and Content-Type are the same header.The Authorization tab: Bearer token and Basic auth
A bearer token is a login token, usually from a login call, sent as Authorization: Bearer <token>. Whoever holds the token gets in, so it must stay secret.
Choose Bearer Token in the Authorization tab and paste the token. The examples show Bearer, Basic, no token and the wrong role.
Example 3 · Bearer, Basic, nothing, and the wrong role
1) GET http://localhost:8080/api/v1/auth/me Authorization: Bearer Token sl_at_admin_7Kq2Vx9P2) GET http://localhost:8080/api/v1/auth/me Authorization: Basic Auth admin@shoplite.test / admin1233) GET http://localhost:8080/api/v1/auth/me Authorization: No Auth4) POST http://localhost:8080/api/v1/products Authorization: Bearer Token sl_at_asha_3Ld8Wm4R (a customer) Body: raw, JSON {"sku":"BK-RDPD","name":"Rich Dad Poor Dad","categoryId":5,"price":299.00,"stock":40}1) Bearer Token
Status: 200 OK
{"email":"admin@shoplite.test","name":"Store Admin","role":"ADMIN"}
2) Basic Auth
Status: 200 OK
{"email":"admin@shoplite.test","name":"Store Admin","role":"ADMIN"}
3) No Auth
Status: 401 Unauthorized
Content-Type: application/problem+json
WWW-Authenticate: Bearer realm="shoplite"
{"type":"https://shoplite.example/problems/unauthorized","title":"Unauthorized","status":401,"detail":"Sign in to use this endpoint.","instance":"/api/v1/auth/me"}
4) Bearer Token of a CUSTOMER, on POST /api/v1/products
Status: 403 Forbidden
{"type":"https://shoplite.example/problems/forbidden","title":"Forbidden","status":403,"detail":"You do not have permission to do this.","instance":"/api/v1/products"}What this code does
- Requests 1 and 2 prove the same identity in two ways. For Basic, Postman sent
Authorization: Basic YWRtaW5Ac2hvcGxpdGUudGVzdDphZG1pbjEyMw==, which is justadmin@shoplite.test:admin123in Base64. - Request 3 has no login, so
401plusWWW-Authenticate: Bearertells you which login to send. Request 4 has a valid login with the wrong role, so the answer is403, not 401.
The Authorization tab fills the Authorization header for you. Basic Auth takes a username and password and sends Authorization: Basic <base64 of username:password>. Base64 is only a way to write bytes as text, not encryption, so Basic auth is safe only over HTTPS. Without the tab you would type Bearer plus a long token into the Headers table by hand and get the space or the word wrong.
Two statuses tell login problems apart. 401 Unauthorized means "I do not know who you are": no token, a wrong token or an expired one. 403 Forbidden means "I know who you are, but you may not do this": logged in, but with the wrong role. A 401 response usually carries a WWW-Authenticate header naming the login type the server expects.
Predict the output
Which status comes back?
Read each request and pick the status.
Round 1 of 2
PUT, PATCH and DELETE requests
PUT, PATCH and DELETE are the write methods for an existing resource: PUT replaces it as a whole, PATCH changes only the fields you send, and DELETE removes it.
In Postman you only change the method dropdown; the Body and Authorization tabs work as for POST. The examples PATCH one field, PUT a whole product and DELETE twice.
Example 4 · PATCH one field, then PUT the whole product
Authorization: Bearer Token sl_at_admin_7Kq2Vx9P on all three, Body: raw, JSON1) PATCH http://localhost:8080/api/v1/products/6 {"stock": 110}2) PUT http://localhost:8080/api/v1/products/6 {"price": 1199.00}3) PUT http://localhost:8080/api/v1/products/6 {"sku":"ACC-AD141","name":"boAt Airdopes 141","categoryId":4,"categoryName":"Accessories","price":1199.00,"stock":110,"isActive":true}1) PATCH /api/v1/products/6
Status: 200 OK
{"id":6,"sku":"ACC-AD141","name":"boAt Airdopes 141","categoryId":4,"categoryName":"Accessories","price":1299.00,"stock":110,"isActive":true}
2) PUT /api/v1/products/6 with only a price
Status: 400 Bad Request
{"type":"https://shoplite.example/problems/validation-failed","title":"Validation failed","status":400,"detail":"The request has 5 invalid fields.","instance":"/api/v1/products/6","errors":[{"field":"sku","message":"is required"},{"field":"name","message":"is required"},{"field":"categoryId","message":"is required"},{"field":"stock","message":"is required"},{"field":"isActive","message":"is required"}]}
3) PUT /api/v1/products/6 with every field
Status: 200 OK
{"id":6,"sku":"ACC-AD141","name":"boAt Airdopes 141","categoryId":4,"categoryName":"Accessories","price":1199.00,"stock":110,"isActive":true}What this code does
- PATCH changed only
stockand kept every other field. The response shows the whole updated product, so you can check it at once. - The PUT with only a price was rejected: PUT means "here is the full product", and five required fields were missing. The full PUT then worked.
Example 5 · DELETE twice
Authorization: Bearer Token sl_at_admin_7Kq2Vx9P1) DELETE http://localhost:8080/api/v1/products/142) DELETE http://localhost:8080/api/v1/products/143) GET http://localhost:8080/api/v1/products/141) DELETE /api/v1/products/14
Status: 204 No Content
2) DELETE /api/v1/products/14 again
Status: 404 Not Found
{"type":"https://shoplite.example/problems/not-found","title":"Resource not found","status":404,"detail":"Product 14 was not found.","instance":"/api/v1/products/14"}
3) GET /api/v1/products/14
Status: 404 Not Found
{"type":"https://shoplite.example/problems/not-found","title":"Resource not found","status":404,"detail":"Product 14 was not found.","instance":"/api/v1/products/14"}What this code does
204 No Contentmeans the delete worked and there is no body to show; Postman's Body tab stays empty.- The second DELETE and the GET both say
404: the product is gone. The final state is the same however many times you delete, which is why DELETE is called idempotent.
PUT needs every field in its body, PATCH can send just {"stock": 110}, and DELETE usually has no body. The usual answers: PUT and PATCH return 200 OK with the updated resource, and DELETE returns 204 No Content with an empty body. A second DELETE of the same id returns 404 Not Found, because there is nothing left to delete.
The common mistake is to send a PATCH-style body with PUT. A careful API rejects it with 400 and lists the missing fields; a careless one silently wipes them. Read the response to know which kind of API you have.
Fixing 415 and 400 responses caused by the wrong body type
415 Unsupported Media Type means the server cannot read the body's format because the Content-Type is wrong. 400 Bad Request means it read the format, but the content is broken or invalid.
The body of the error response usually says which one it is. The example shows four rejected bodies and the fix for each.
Example 6 · Four rejected bodies and the fix
POST http://localhost:8080/api/v1/products (Bearer Token sl_at_admin_7Kq2Vx9P)1) Body: raw, Text (the dropdown was never changed)2) Body: form-data (key sku = BK-RDPD)3) Body: raw, JSON with a comma after "stock": 404) Body: raw, JSON with "price": "299.00" (text, not a number)5) Body: raw, JSON fixed:{ "sku": "BK-RDPD", "name": "Rich Dad Poor Dad", "categoryId": 5, "price": 299.00, "stock": 40}1) Body: raw, Text
Status: 415 Unsupported Media Type
{"type":"https://shoplite.example/problems/unsupported-media-type","title":"Unsupported media type","status":415,"detail":"Content-Type text/plain is not supported. Send application/json.","instance":"/api/v1/products"}
2) Body: form-data
Status: 415 Unsupported Media Type
{"type":"https://shoplite.example/problems/unsupported-media-type","title":"Unsupported media type","status":415,"detail":"Content-Type multipart/form-data is not supported. Send application/json.","instance":"/api/v1/products"}
3) Body: raw, JSON, with a trailing comma
Status: 400 Bad Request
{"type":"https://shoplite.example/problems/malformed-request","title":"Malformed request","status":400,"detail":"The request body is not valid JSON.","instance":"/api/v1/products"}
4) Body: raw, JSON, with "price": "299.00"
Status: 400 Bad Request
{"type":"https://shoplite.example/problems/malformed-request","title":"Malformed request","status":400,"detail":"Field price must be a number.","instance":"/api/v1/products"}
5) Body: raw, JSON, fixed
Status: 201 Created
{"id":16,"sku":"BK-RDPD","name":"Rich Dad Poor Dad","categoryId":5,"categoryName":"Books","price":299.00,"stock":40,"isActive":true}What this code does
- Requests 1 and 2 never reached the JSON check: the
Content-Typewastext/plainormultipart/form-data, so the server answered415and named the type it wants. - Request 3 had the right type but broken JSON (the trailing comma), and request 4 had valid JSON with a value of the wrong type. Both are
400 Malformed request, and thedetailsays what is wrong.
Match the status to the cause:
- 415 with
text/plain→ the Body dropdown still says Text; switch it to JSON. - 415 with
multipart/form-dataorx-www-form-urlencoded→ you picked form-data; use raw → JSON. - 400
Malformed request→ the body cannot be read: a syntax slip (a trailing comma, single quotes, a missing quote or brace) or a value of the wrong type, such as a price sent as the text"299.00"instead of the number299.00. - 400
Validation failed→ the JSON is fine but a value breaks a rule, such as a price of-5; theerrorslist names every broken field.
Fix the error
Fix the rejected request
Read the error, then pick the line that fixes it.
Round 1 of 2
The server cannot read the body format.
Error
415 Unsupported Media Type "detail": "Content type 'text/plain' is not supported."
Next step
Check your understanding
12 quick questions