Python Forum

Full Version: Powershell Session translation to Python; Session code seems to not work
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The following are what I have in powershell

# Get a SIM Session Cookie
Invoke-WebRequest -UseDefaultCredentials -UseBasicParsing -Uri https://maxis-service.domain.com/users/whoami -SessionVariable sim

#Send Post to Create Job
$response = Invoke-RestMethod -UseDefaultCredentials -UseBasicParsing -WebSession $sim -Uri https://maxis-service.domain.com/jobs -Method Post -Body $json -ContentType "application/json"

I believe this should translate to a session with requests, but if I am wrong please let me know.

I tried using the following in Python after a lot of google, but it doesn't seem to work like I thought it did (similar to what powershell is doing):

    sessionURL = 'https://maxis-service.domain.com/users/whoami'
    simURL = 'https://maxis-service.domain.com/jobs'    
    s = requests.Session()
    sr = s.get(sessionURL,verify=False)
    cookie = s.cookies
    print(cookie)
    dr = s.post(simURL,data=rawJson,verify=False)
cookie prints the expected cookie, but then the post request gives a 401; is there a reason that python gets a 401? am I not using the session correctly?
I should probably have expanded here is the output when I print dr as well:

<RequestsCookieJar[<Cookie cookie_cookie=1b41fd0a9722b852 for maxis-service.domain.com/>]>
<Response [401]>

So I can't understand why the post call using the session is giving a 401