Menu
Coddy logo textTech

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/users

First 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 icon

Challenge

Expert

Send real JSON and prove it was labelled correctly.

  1. Start the capture listener on port 9125
  2. POST the body {"name":"ada"} to /users, declaring it as JSON
  3. Print the Content-Type line from req.txt

Expected output:

Content-Type: application/json

Try it yourself

Terminal
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Networking with the Terminal

Practice on your own: Terminal playground