Need a simple curl POST example – can anyone share a quick snippet? or Struggling with a curl POST example – any

24 Replies, 1603 Views

"Hey folks,

Struggling to get a curl POST example working for an API test. Every time I try, I either get a 400 error or the server just ignores my data.

Can someone share a quick snippet for a basic curl POST example with JSON data? Like, the simplest one possible—just need to send `{"name": "test"}` to `https://example.com/api`.

I’ve tried a bunch of flags, but maybe I’m missing something obvious.

Also, how do you usually format the headers? Do you always need `-H 'Content-Type: application/json'`?

Thanks in advance!

(PS: If you’ve got a better curl POST example for beginners, I’m all ears.)"
Here’s your first reply:
Hey! Here’s a dead-simple curl POST example for your case:

```
curl -X POST https://example.com/api \
-H 'Content-Type: application/json' \
-d '{"name": "test"}'
```

The `-X POST` is optional (curl defaults to POST with `-d`), but I like being explicit.

And yes, you *usually* need the `Content-Type` header—some APIs will ignore your data without it.

If you’re still stuck, try `-v` to see the full request/response. Helps debug those 400 errors!
400 errors are the worst. Here’s what works for me 99% of the time:

```
curl -H "Content-Type: application/json" -d '{"name":"test"}' https://example.com/api
```

Pro tip: If the API needs auth, slap on `-H 'Authorization: Bearer YOUR_TOKEN'`.

Also, check out Postman or Insomnia if you’re tired of debugging curl commands. They’re lifesavers for API testing.
Yo, had the same issue last week! Here’s the magic combo:

```
curl -X POST https://example.com/api \
-H "Content-Type: application/json" \
-d '{"name": "test"}'
```

If it’s still failing, maybe the API expects a trailing slash or something? Try `https://example.com/api/`.

Also, `jq` is handy for pretty-printing JSON responses if you’re on Linux/Mac.
Thanks everyone! The `-v` flag was a game-changer—turns out the API was rejecting my request because I forgot the trailing slash.

This worked:

```
curl -X POST https://example.com/api/ \
-H 'Content-Type: application/json' \
-d '{"name": "test"}'
```

Still weird that the server cares about the slash, but hey, it’s working now.

Quick Q: How do I handle nested JSON? Like `{"user": {"name": "test"}}`? Same format?
For a beginner-friendly curl POST example, this should work:

```
curl -d '{"name":"test"}' -H "Content-Type: application/json" -X POST https://example.com/api
```

Headers *matter*—some APIs freak out if you skip `Content-Type`.

If you’re on Windows, watch out for quote issues. Sometimes swapping `'` for `"` around the JSON fixes it.
Struggling with curl POST too? Here’s the bare minimum:

```
curl -d '{"name":"test"}' -H "Content-Type: application/json" https://example.com/api
```

Fun fact: `-X POST` isn’t needed if you use `-d`.

If you’re getting ignored, try `-v` to see if the request even leaves your machine.
Here’s my go-to curl POST example for APIs:

```
curl -X POST \
https://example.com/api \
-H 'Content-Type: application/json' \
-d '{"name": "test"}'
```

Bonus: If the API uses HTTPS and you’re getting cert errors, add `-k` (but only for testing—don’t ship that!).
Short and sweet:

```
curl -X POST https://example.com/api -H "Content-Type: application/json" -d '{"name":"test"}'
```

If 400 persists, maybe the server expects `Accept: application/json` too? Some APIs are picky.



Users browsing this thread: 1 Guest(s)