G
G
Grigory Boev2020-08-22 20:41:00
Python
Grigory Boev, 2020-08-22 20:41:00

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:

spoiler
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"
});

In the browser console, this code works, the answer is 200 + data
Question: how to make such a request using python ?
Tried like this:
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

I receive cookies through selenium while
cookies = browser.get_cookies()
I can’t figure out how to form the header. If added as a dictionary,
spoiler
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'
    }

then I get an error
expected string or bytes-like object

If as a string,
spoiler
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'
    """

then I get an error
'str' object has no attribute 'items'

Tell me, where did you go wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Karbivnichy, 2020-08-22
@ProgrammerForever

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 question

Ask a Question

731 491 924 answers to any question