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

12 Replies, 730 Views

Kinda random, but are you sure the server expects JSON? Some old-school APIs want form-encoded data. Try:

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

Also, slap on some basic auth if needed:

```python
auth = ('user', 'pass')
response = requests.post(url, json=..., auth=auth)
```

For timeouts, yeah, always set one. Retries? I just wrap it in a for-loop with sleeps. Not fancy but ¯\_(ツ)_/¯

Messages In This Thread



Users browsing this thread: 1 Guest(s)