Sending JSON
Lesson 35 of 47 in Coddy's Networking with the Terminal course.
Most APIs today want JSON, not form encoding. Two things have to change, and forgetting the second is the single most common mistake in this whole area.
curl -s -H 'Content-Type: application/json' \
-d '{"name":"ada"}' http://127.0.0.1:9125/usersFirst the body becomes JSON. Second you must declare it with a Content-Type header, because curl still labels the body as form data otherwise. The server then tries to parse JSON as key=value pairs and rejects it, usually with a 400.
So when an API says your JSON is invalid and it looks perfectly valid to you, check the header before you check the JSON.
Mind the quoting too. JSON is full of double quotes, so wrap the whole body in single quotes and the shell will leave it alone.
Challenge
ExpertSend real JSON and prove it was labelled correctly.
- Start the capture listener on port 9125
- POST the body
{"name":"ada"}to/users, declaring it as JSON - Print the Content-Type line from
req.txt
Expected output:
Content-Type: application/jsonTry 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
4Is It Reachable?
ICMP and pingReading Ping OutputWhen Ping LiesRefused or Timed OutRecap: Reachability7Sending Data
POST with -dWhat -d Actually SendsForm EncodingSending JSONPUT and DELETERecap: Writing DataPractice on your own: Terminal playground