Answer the question
In order to leave comments, you need to log in
How to correctly send a request with headers and cookies?
Good afternoon. I started to deal with python, I'm making a parser. In the browser, in the console, I looked at the headers and did "Copy as fetch". Got something like this:
fetch("https://site.com/api/", {
"headers": {
"accept": "*/*",
"accept-language": "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7",
"id": "12345",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin"
},
"referrer": "https://site.com/api",
"referrerPolicy": "no-referrer-when-downgrade",
"body": null,
"method": "GET",
"mode": "cors",
"credentials": "include"
});
def get_data(cookies, headers):
base_url = 'https://site.com'
method_url = '/api'
with requests.Session() as session:
for cookie in cookies:
session.cookies.update(cookie)
url = base_url + method_url
response = session.get(url=url, headers=headers)
return response
cookies = browser.get_cookies()
headers = {
"accept": "*/*",
"accept-language": "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7",
"id": id,
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
'referrer': 'https://site.com,
'referrerPolicy': 'no-referrer-when-downgrade',
'method': 'GET',
'mode': 'cors',
'credentials': 'include'
}
expected string or bytes-like object
headers = """
"accept": "*/*",
"accept-language": "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7",
"id": id,
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
'referrer': 'https://site.com,
'referrerPolicy': 'no-referrer-when-downgrade',
'method': 'GET',
'mode': 'cors',
'credentials': 'include'
"""
'str' object has no attribute 'items'
Answer the question
In order to leave comments, you need to log in
You can also do this - "Copy as cURL", then go to curl.trillworks.com and convert to Python code.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question