简体   繁体   中英

Python Requests HTTP POST response cookie missing / hiding fields

new to using Python requests lib, and looking for a little help around accessing cookies...

I am unable to get access to all of the fields within a cookie that I get from the following code using the Requests library - similar code from GoLang or Postman all work fine, but for some reason i am missing a few key fields that I need from Python-land. Sample code is as follows:

import requests

# Host base URL
host = 'sampleurl.link/endpoint'
# Username and password for login to API endpoint
credentials = "username=sausage123%40spaniel.org&password=Passw0rd"

ses = requests.Session()

authString = ''

def auth(host):
    payload = credentials
    headers = {
            'Content-Type': "application/x-www-form-urlencoded",
        }

    ses.post("https://" + host + "/auth", data=payload, headers=headers)

    cookies = ses.cookies.get_dict()
    print(cookies.items())
    authString = "; ".join([str(x)+"="+str(y) for x,y in cookies.items()])
    print(authString)

auth(host)

The output is as follows:

[('systemid3', 'eSgRCbaH2EWyBVbRyfBS7xfftYCAqE-BRaon1Uc350pi14qTVgsmDXLrK9TDJvPsKmAzgw==')]

However, from the same API call in GoLang or Postman equiv. i get all of the required fields including path and expires:

systemid3=eSgRCbaH2EWyBVbRyfBS7xfftYCAqE-BRaon1Uc350pi14qTVgsmDXLrK9TDJvPsKmAzgw==; Path=/; Expires=Sat, 21 Sep 2019 09:37:46 GMT

Note that Postman also gives me the same 'Path' and 'Expires' fields etc.

How do i get Requests lib to give me access to all of the fields in the cookie, not just the first 2? Why is it hiding / removing the othe cookie fields?

Thanks in advance!

It's because you're using .items() , it only gets cookie name and value

You can access other parts like this:

for cookie in s.cookies:
   print(cookie.name)
   print(cookie.value)
   print(cookie.expires)
   print(cookie.path)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM