[b]"How to properly use requests post in Python for API calls?"[/b] or [b]"What's the best way to handle a request

12 Replies, 673 Views

Had this exact headache yesterday! Two things:

1. Some APIs need the `Accept` header too:
```python
headers = {'Accept': 'application/json'}
```

2. Your `json=` might be fine, but if the server’s weird, try `json.dumps()` + `data=`:

```python
import json
response = requests.post(url, data=json.dumps({'key': 'value'}), headers=headers)
```

For retries, `urllib3` has a built-in retry thingy.

---

Oh wow, thanks y’all! Didn’t realize headers were such a big deal—adding `Content-Type` fixed it for one API.

But now I’m getting a 403 on another endpoint. Could it be a auth thing even if the docs don’t mention it?

Also, `tenacity` looks dope for retries—gonna try that next.

Side note: logging.debug was a game-changer. Why didn’t I know about that sooner?!

Thanks again 🙌

Messages In This Thread



Users browsing this thread: 1 Guest(s)