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

18 Replies, 1819 Views

Here’s a casual forum post for you:

---

Hey everyone!

I’m kinda stuck trying to figure out a *curl post example* for sending some data. Like, how do I even format this properly?

If I wanna send JSON, is it just:
```bash
curl -X POST https://example.com/api -H "Content-Type: application/json" -d '{"key":"value"}'
```
Or am I missing something?

Also, what if it’s form data instead? Like, do I use `-F` or still `-d`? A quick *curl post example* for both cases would be *so* helpful.

Thanks in advance! Y’all are lifesavers 🙏

---

Let me know if you want it tweaked! Kept it real and a bit messy, like an actual forum post.
Your curl post example for JSON looks spot on! That’s exactly how I do it. For form data, you’d use `-F` instead of `-d`. Here’s how:

```bash
curl -X POST https://example.com/api -F "key=value" -F "file=@path/to/file"
```

If you’re dealing with URL-encoded form data, stick with `-d` but add the header `-H "Content-Type: application/x-www-form-urlencoded"`.

Btw, Postman is a great tool to test these requests before running them in curl.
Yo, you got the JSON part right! For form data, it depends. If it’s multipart (like uploading a file), use `-F`. If it’s just key-value pairs, `-d` works but with the right header.

Example for multipart:
```bash
curl -X POST https://example.com/upload -F "name=foo" -F "image=@pic.jpg"
```

For URL-encoded:
```bash
curl -X POST https://example.com/api -d "param1=value1&param2=value2" -H "Content-Type: application/x-www-form-urlencoded"
```

Hope that helps!
Dude, your curl post example is correct for JSON. For form data, it’s a bit different. Use `-F` for multipart forms (like file uploads) and `-d` for URL-encoded stuff.

Here’s a quick cheat:
- JSON: `-d '{"key":"value"}'` + JSON header
- Multipart: `-F "field=value"`
- URL-encoded: `-d "field=value"` + URL-encoded header

Also, check out `curl --help` or the man pages for more flags.
Your JSON example is perfect! For form data, it’s all about the headers.

If you’re sending URL-encoded form data:
```bash
curl -X POST https://example.com/api -d "key1=value1&key2=value2" -H "Content-Type: application/x-www-form-urlencoded"
```

For multipart (files, etc.):
```bash
curl -X POST https://example.com/api -F "key=value" -F "file=@filename"
```

Pro tip: Use `-v` to see the full request and debug issues.
Wow, thanks everyone! This is super helpful. I tried the multipart example with `-F` and it worked perfectly.

One follow-up: What if the API requires authentication? Like, do I just add `-H "Authorization: Bearer token"` to the same curl post example?

Also, big shoutout to the Postman suggestion—definitely gonna try that for testing. Y’all are the best!
Not bad! Your curl post example for JSON is correct. For form data, it’s a bit trickier.

If it’s simple key-value pairs (no files), use `-d` with the URL-encoded header:
```bash
curl -X POST https://example.com/api -d "param1=value1" -H "Content-Type: application/x-www-form-urlencoded"
```

For files or multipart, `-F` is your friend:
```bash
curl -X POST https://example.com/upload -F "name=test" -F "doc=@document.pdf"
```

Try https://reqbin.com/ to test these online.
Hey! Your curl post example for JSON is on point. For form data, here’s the breakdown:

- Multipart (files, etc.): Use `-F` like this:
```bash
curl -X POST https://example.com/api -F "field=value" -F "file=@path/to/file"
```

- URL-encoded: Use `-d` with the right header:
```bash
curl -X POST https://example.com/api -d "key=value" -H "Content-Type: application/x-www-form-urlencoded"
```

If you’re unsure, `curl --trace-ascii debug.txt` helps log the request.
Your JSON example is correct! For form data, it depends on the type.

For multipart/form-data (files, etc.):
```bash
curl -X POST https://example.com/api -F "name=foo" -F "image=@photo.png"
```

For URL-encoded:
```bash
curl -X POST https://example.com/api -d "key1=value1&key2=value2" -H "Content-Type: application/x-www-form-urlencoded"
```

P.S. Insomnia or Postman are great for testing these before running curl.
Nice curl post example for JSON! For form data, here’s the deal:

- Use `-F` for multipart (files, etc.):
```bash
curl -X POST https://example.com/upload -F "username=test" -F "avatar=@image.jpg"
```

- Use `-d` for URL-encoded:
```bash
curl -X POST https://example.com/login -d "user=me&pass=123" -H "Content-Type: application/x-www-form-urlencoded"
```

If you’re stuck, `curl --verbose` shows the raw request.



Users browsing this thread: 1 Guest(s)