Lesson 2
Headers, Methods, and Bodies
Map `-X`, `-H`, and `-d` options to a clear HTTP request model.
Most cURL-to-code conversion revolves around three concepts: method, headers, and body.
Method
-X or --request sets the HTTP method:
curl -X PATCH https://api.example.com/users/42
Without -X, cURL usually sends GET. With -d, cURL commonly sends POST.
Headers
-H or --header adds headers:
curl -H "Accept: application/json" -H "X-Request-Id: demo-1" https://api.example.com
Headers are case-insensitive by HTTP rules, but application code should preserve names clearly for readability.
Body
-d, --data, --data-raw, and --data-binary attach body content:
curl -H "Content-Type: application/json" -d '{"name":"Ada"}' https://api.example.com/users
The body is just bytes. The Content-Type header tells the server how to interpret those bytes.
Form and JSON are different
These are not equivalent:
curl -d "name=Ada&role=admin" https://api.example.com/users
curl -H "Content-Type: application/json" -d '{"name":"Ada","role":"admin"}' https://api.example.com/users
The first is form-style data. The second is JSON. A converter can translate both, but your application still needs the server to expect the same content type.