How to Use curl to Send JSON Data in a POST Request?

22 Replies, 1757 Views

Hey folks,

So, I was trying to figure out how to curl send json in a POST request, and man, it took me a hot minute to get it right. Like, why is it so confusing at first glance? Anyway, here's the deal:

You gotta use the `-H` flag to set the Content-Type to `application/json`, and then slap your JSON data in with `-d`. Something like this:

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

But here’s the kicker—if your JSON is in a file, you can just do `-d @filename.json` instead of typing it all out. Lifesaver, honestly.

Also, pro tip: if you mess up the quotes or brackets, curl will just silently fail. So double-check that JSON, folks.

Anyway, hope this helps someone avoid the headache I went through. Cheers! 🍻

Messages In This Thread
How to Use curl to Send JSON Data in a POST Request? - by - 16-04-2024, 10:37 PM



Users browsing this thread: 1 Guest(s)