How do you send GET and POST requests with curl?
curl URL sends a GET. For a POST with JSON use curl -X POST -H 'Content-Type: application/json' -d '{...}' URL. Adding -d already makes the request a POST, and -i or -v shows the response headers.
Explain it simply
curl is a command-line browser that you can script. It is the quickest way to test an API without Postman.
A plain curl https://api.example.com/users sends a GET and prints the body.
To send JSON:
curl -X POST https://api.example.com/users -H "Content-Type: application/json" -d '{"name":"Ria"}'
-H adds a header and -d supplies the body. -d alone switches the method to POST, so -X POST is often optional. For form data use -d "a=1&b=2", and for a file upload -F file=@photo.png.
Helpful flags: -i includes response headers in the output, -v shows the whole conversation including the request, -u user:pass sends basic authentication, -H "Authorization: Bearer TOKEN" sends a token, and -s silences the progress meter in scripts.
Wrap the JSON in single quotes so the shell does not eat the double quotes.
Example
curl -s -X POST https://httpbin.org/post \
-H "Content-Type: application/json" \
-d '{"name":"Ria"}' | head -5
curl -i https://example.com | head -1
# HTTP/2 200What to say in the interview
"curl with no options does a GET. For POST I pass -d with the body and a Content-Type header, -H for tokens, and -i or -v to inspect headers. -s is handy in scripts, and I put JSON in single quotes."
Common mistakes
- Forgetting the Content-Type header so the server cannot parse the JSON.