How do I properly use curl --post to send data in a request? or What's the correct syntax for curl --

18 Replies, 1800 Views

Hey folks!

So I'm trying to figure out how to properly use curl --post to send some data, but I’m kinda stuck. Like, what’s the right syntax for form data?

I tried something like:
`curl --post "name=foo&age=25" https://example.com`
But it’s not working?? Am I missing something obvious?

Also, what’s the diff between curl --post and just -X POST? Seen both around and got confused.

Oh, and if anyone’s got tips for sending JSON with curl --post, that’d be awesome too. My attempts keep getting rejected lol.

Thanks in advance! 🙏
Hey! You’re close, but curl --post isn’t the correct flag—it’s actually `-d` or `--data` for form data. Try this:
`curl -d "name=foo&age=25" https://example.com`

For JSON, use `-H "Content-Type: application/json"` and `-d '{"name":"foo","age":25}'`.

And `-X POST` explicitly sets the method, while `-d` implies POST. Hope that helps!
lol yeah curl --post isn’t a thing, mate. You wanna use `-d` or `--data-urlencode` for form stuff.

For JSON, slap on a header like `-H "Content-Type: application/json"` and then your JSON data.

Also, check out Postman if you’re struggling—way easier to visualize requests.
The correct syntax for form data with curl is:
`curl -X POST -d "name=foo&age=25" https://example.com`

For JSON:
`curl -X POST -H "Content-Type: application/json" -d '{"name":"foo","age":25}' https://example.com`

`-X POST` is just explicit, while `-d` defaults to POST. No such thing as curl --post, tho.
Dude, curl --post doesn’t exist. You’re mixing flags. Use `-d` for data.

Example for form data:
`curl -d "name=foo&age=25" https://example.com`

For JSON, add the content-type header:
`curl -d '{"name":"foo"}' -H "Content-Type: application/json" https://example.com`

Pro tip: Use `curl --help` or check the man pages.
Ohhh, that makes so much sense now! Thanks, everyone—didn’t realize curl --post wasn’t a thing.

Tried the `-d` flag with JSON and it worked like a charm:
`curl -H "Content-Type: application/json" -d '{"name":"foo"}' https://example.com`

Quick follow-up: What’s the best way to handle auth headers with curl? Like, if I need to add a token?

Also, shoutout to the Postman suggestion—def gonna check that out.
Ah, the classic curl confusion! `curl --post` isn’t valid—use `-X POST` or just `-d`.

For form data:
`curl -d "name=foo&age=25" https://example.com`

For JSON, don’t forget the header:
`curl -X POST -H "Content-Type: application/json" -d '{"name":"foo"}' https://example.com`

Also, try https://reqbin.com/ to test requests before curling.
You’re missing the `-d` flag! curl --post isn’t a real option.

Form data:
`curl -d "name=foo&age=25" https://example.com`

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

`-X POST` is optional here since `-d` implies POST.
Nope, no such thing as curl --post. Use `-d` or `--data` instead.

For form data:
`curl -d "name=foo&age=25" https://example.com`

For JSON, add the header:
`curl -H "Content-Type: application/json" -d '{"name":"foo"}' https://example.com`

Bonus: Use `-v` to see verbose output and debug.
curl --post? Nah, fam. It’s `-d` for data.

Form data:
`curl -d "name=foo&age=25" https://example.com`

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

`-X POST` is redundant with `-d`, but some folks like being explicit.



Users browsing this thread: 1 Guest(s)