One API I'm currently using specifies that I need a special content-type string. I don't know how do I set this in python-requests library
Asked
Active
Viewed 1.3e+01k times
57
-
possible duplicate of [Post JSON using Python Requests](http://stackoverflow.com/questions/9733638/post-json-using-python-requests) – Tim Sep 18 '15 at 11:52
-
2Just set the header? `requests` will set the `Content-Type` only if you use `data`, `files` or `json` as arguments to the `requests.post()` call, and you can always override that with the `headers` dictionary. – Martijn Pieters Sep 18 '15 at 11:57
1 Answers
109
import requests
headers = {'Content-type': 'content_type_value'}
r = requests.get(url, headers=headers)

Vikas Ojha
- 6,742
- 6
- 22
- 35
-
7For anyone else who ended up here because of googling an error message: Note that usually, you don't want to explicitely set the header. The requests library will do it for you. See https://stackoverflow.com/a/57479286/4629950 . The answer from Vikas Ohja is still correct because in this case the question was explicitely about setting it manually. – Thomas Aug 18 '21 at 09:07