Adding headers to requests module

后端 未结 2 955
滥情空心
滥情空心 2020-11-27 14:46

Earlier I used httplib module to add a header in the request. Now I am trying the same thing with the requests module.

This is the python re

2条回答
  •  情书的邮戳
    2020-11-27 15:20

    From http://docs.python-requests.org/en/latest/user/quickstart/

    url = 'https://api.github.com/some/endpoint'
    payload = {'some': 'data'}
    headers = {'content-type': 'application/json'}
    
    r = requests.post(url, data=json.dumps(payload), headers=headers)
    

    You just need to create a dict with your headers (key: value pairs where the key is the name of the header and the value is, well, the value of the pair) and pass that dict to the headers parameter on the .get or .post method.

    So more specific to your question:

    headers = {'foobar': 'raboof'}
    requests.get('http://himom.com', headers=headers)
    

提交回复
热议问题