[b]"Need a simple curl post example – how do I send data with curl?"[/b] or [b]"Can someone share a basic curl pos

16 Replies, 885 Views

"Need a simple curl post example – how do I send data with curl?"

Hey folks,

I'm kinda stuck here. Trying to send some data via curl, but every time I mess up the syntax.

Can someone share a basic curl post example? Like, just a simple one with JSON data?

I tried `curl -X POST https://example.com/api` but it’s not working, and I’m not sure where to put the JSON payload.

A quick working snippet would save me hours of googling lol.

Thanks in advance!

---

*or*

"Struggling with curl post example – any quick working snippet?"

Yo,

Total noob here. Need a curl post example to hit an API endpoint.

Something like sending `{"name": "test"}` to a URL.

I keep getting 400 errors... am I missing headers?

Pls help a fellow dev out with a quick example!

Cheers.
Here's a quick curl post example for ya:

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

You gotta include the `-H` flag for headers (JSON needs it) and `-d` for the data.

If you're still getting 400s, check if the endpoint needs auth—maybe add `-H "Authorization: Bearer YOUR_TOKEN"`.
Yo, had the same issue last week!

For a simple curl post example, this works:

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

Pro tip: Use https://reqbin.com/ to test your requests before running curl. Saves a ton of time!

Also, 400 usually means bad data or missing headers. Double-check your JSON formatting.
Hey! Here’s a basic curl post example with JSON:

```
curl -X POST https://example.com/api \
--header "Content-Type: application/json" \
--data '{"username":"foo","password":"bar"}'
```

If it’s still failing, try `-v` to see verbose output—helps debug the issue.

Also, Postman (https://www.postman.com/) is great for testing APIs before writing curl commands.
Dude, you’re missing the headers! Here’s how I do it:

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

Sometimes APIs are picky about `Accept` headers too.

If you’re on Windows, watch out for quotes—use double quotes outside and escape the JSON ones.
Here’s a foolproof curl post example:

```
curl -X POST https://example.com/api \
-H 'Content-Type: application/json' \
-d '{"field1":"value1", "field2":"value2"}'
```

Bonus: Use `jq` to prettify JSON responses like this:

```
curl ... | jq
```

If you’re stuck, paste your error—we’ll figure it out!
Wow, thanks everyone! Didn’t realize the headers were that important.

Tried the first curl post example and it worked!

But now I’m getting a 401—guess I need auth. How do I add a basic auth token?

Also, jq looks dope, gonna try that next.

Cheers!
400 errors? Classic.

Try this curl post example:

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

Put your JSON in a file (`data.json`) to avoid quote issues.

Also, check if the API docs mention required fields—sometimes it’s not the syntax but missing data.
For a dead-simple curl post example:

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

If it’s still not working, maybe the endpoint’s picky. Try `-v` to see the full request/response.

Oh, and https://httpie.io/ is a nicer alternative to curl for quick testing.



Users browsing this thread: 1 Guest(s)