Consider this HTTP POST request snippet:
POST /api/data HTTP/1.1
Host: example.com
Content-Type: application/json
Accept: application/json
{"name": "Alice", "age": 30}What is the value of the Content-Type header?
The Content-Type header tells the server what format the request body is in.
The Content-Type header is set to application/json because the request body contains JSON data.
Given this HTTP GET request header:
GET /api/users HTTP/1.1 Host: example.com Accept: application/xml
What format does the client expect the server to respond with?
The Accept header tells the server what content types the client can handle.
The Accept header is application/xml, so the client expects XML format in the response.
Look at this HTTP POST request:
POST /submit HTTP/1.1
Host: example.com
Accept: application/json
{"username": "bob", "score": 42}Why might the server reject this request?
Check if the server knows what format the body is in.
The request lacks a Content-Type header, so the server cannot interpret the JSON body correctly and may reject it.
In an HTTP request, the client sets the header Accept: */*. What does this mean?
Think about what the wildcard '*' means in this context.
The Accept header value '*/*' means the client can accept any content type the server sends back.
Consider this Python code that simulates checking headers:
headers = {
"Content-Type": "application/json",
"Accept": "application/xml"
}
if headers.get("Content-Type") == "application/json" and headers.get("Accept") == "application/xml":
result = "Send JSON, expect XML"
else:
result = "Headers mismatch"
print(result)What will be printed?
headers = {
"Content-Type": "application/json",
"Accept": "application/xml"
}
if headers.get("Content-Type") == "application/json" and headers.get("Accept") == "application/xml":
result = "Send JSON, expect XML"
else:
result = "Headers mismatch"
print(result)Check the dictionary keys and values carefully.
Both headers match the conditions, so the code prints 'Send JSON, expect XML'.