How to Properly Use curl with Headers for API Requests?

22 Replies, 1775 Views

Hey! Just wanted to share a quick tip for curl with headers. If you’re dealing with APIs that require CSRF tokens or other dynamic headers, you can use a combination of `grep` and `sed` to extract and reuse them in your curl commands.

For example:

```
TOKEN=$(curl -s -H "Accept: application/json" https://api.example.com/login | grep -oP '"token":"\K[^"]+')
curl -H "Authorization: Bearer $TOKEN" https://api.example.com/data
```

This is super handy for automating API workflows.
Curl with headers is a must-know for API work. One thing I’ve found helpful is using `--compressed` to handle gzipped responses. Combine it with headers like this:

`curl -H "Accept-Encoding: gzip" --compressed https://api.example.com/data`

Also, if you’re working with REST APIs, https://swagger.io/ is a great resource for understanding how headers are used in different endpoints.



Users browsing this thread: 1 Guest(s)