Custom Headers
Lesson 28 of 47 in Coddy's Networking with the Terminal course.
-H adds a header to your request. This is how you tell a server who you are and what you can accept:
curl -s -H 'Accept: application/json' http://127.0.0.1:9118/You can repeat -H as often as you like. The common ones are Accept (what format you want back), Content-Type (what format you are sending), and Authorization (your credentials).
The best way to learn what curl actually sends is to look. Put a listener in the way that answers the request and saves it to a file:
printf 'HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok' \
| nc -l -p 9118 > req.txt &Now req.txt holds your exact request: the request line, every header, and the body. It is the single most useful debugging trick in this course.
Challenge
ExpertSend a header, then prove it arrived.
- Start a listener on port 9118 that replies
HTTP/1.1 200 OKand saves the request intoreq.txt - Make a request carrying the header
X-Course: networking - Print that header line back out of
req.txt
Expected output:
X-Course: networkingSearch case insensitively, and discard curl's own output so only the header prints.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Networking with the Terminal
6curl in Depth
Seeing the HeadersSaving ResponsesCustom HeadersStatus and Timingcurl Exit CodesRecap: curl FlagsPractice on your own: Terminal playground