How to Properly POST JSON Data Using cURL? or What’s the Correct Way to POST JSON with cURL?

18 Replies, 1378 Views

"What’s the Correct Way to POST JSON with cURL?"

Hey folks,

I keep messing up when trying to post json curl requests. Like, I *think* I’m doing it right, but the server keeps yelling at me.

Here’s what I’m doing:
```bash
curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' http://example.com/api
```
But sometimes it just... doesn’t work? Am I missing something obvious?

Also, do I *really* need the `-X POST`? Feels redundant, but I see it everywhere. And what if my json has quotes inside? Do I need to escape them?

Pls halp, my sanity is slipping. 🙃

(Also, if you’ve got a better one-liner to post json curl, drop it below!)
Ah, the classic post json curl struggle! Your command looks mostly correct, but yeah, escaping quotes can be a pain. If your JSON has nested quotes, try wrapping the whole thing in single quotes and escaping the inner doubles with a backslash, like this:

```bash
curl -H "Content-Type: application/json" -d '{\"key\":\"value with \\\"quotes\\\" inside\"}' http://example.com/api
```

And nah, you don’t *need* `-X POST` if you’re using `-d`, since curl defaults to POST with data. But it doesn’t hurt to be explicit.
Dude, I feel you. I wasted hours on this until I found `jq` to format my JSON before posting it with curl.

Try this:
```bash
echo '{"key":"value"}' | jq -c . | curl -H "Content-Type: application/json" -d @- http://example.com/api
```

`jq` ensures your JSON is valid, and `-d @-` reads from stdin. Lifesaver for messy JSON.
For escaping quotes, I just use `printf` to avoid the headache:

```bash
printf '{"key":"%s"}' "value with \"quotes\"" | curl -H "Content-Type: application/json" -d @- http://example.com/api
```

Also, check if the server expects pretty-printed JSON. Some APIs are picky.
Pro tip: Use `--data-binary` instead of `-d` if your JSON has newlines or special chars.

```bash
curl -X POST -H "Content-Type: application/json" --data-binary @file.json http://example.com/api
```

Saves you from escaping hell. And yeah, `-X POST` is optional but good for readability.
If you’re on Windows, escaping is even worse. Use PowerShell’s `Invoke-RestMethod` instead:

```powershell
Invoke-RestMethod -Uri http://example.com/api -Method Post -Body '{"key":"value"}' -ContentType 'application/json'
```

Way cleaner than fighting with curl’s escaping rules.
Wow, thanks for all the tips! Didn’t realize `jq` could save me from JSON formatting hell. Tried the `printf` method and it worked like a charm.

Still getting a 400 error with nested quotes though—maybe the server’s picky. Gonna test with `-v` to see what’s up.

Also, Postman looks slick for debugging. Def gonna try that next. Y’all are legends! 🙌
You might wanna check if the server’s returning errors. Add `-v` to your curl command to see the full request/response.

```bash
curl -v -H "Content-Type: application/json" -d '{"key":"value"}' http://example.com/api
```

Sometimes the server’s error message tells you exactly what’s wrong.
For quick testing, I use Postman or Insomnia to craft the request first, then copy the curl command.

But if you’re stuck with post json curl, try this:

```bash
curl -H "Content-Type: application/json" -d "$(jq -c . <<< '{"key":"value"}')" http://example.com/api
```

`jq` validates the JSON, and `<<<` avoids file clutter.
If your JSON is huge, save it to a file and use `-d @filename.json`.

```bash
curl -X POST -H "Content-Type: application/json" -d @data.json http://example.com/api
```

No escaping needed, and way easier to edit. Also, `-X POST` is optional but I keep it for clarity.



Users browsing this thread: 1 Guest(s)