Python Forum
Powershell Session translation to Python; Session code seems to not work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Powershell Session translation to Python; Session code seems to not work
#1
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?
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  using PowerShell from Python script for mounting shares tester_V 8 511 Mar-12-2024, 06:26 PM
Last Post: tester_V
  hi need help to make this code work correctly atulkul1985 5 773 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  save a session in idle akbarza 5 754 Nov-06-2023, 08:36 AM
Last Post: snippsat
  newbie question - can't make code work tronic72 2 676 Oct-22-2023, 09:08 PM
Last Post: tronic72
  PowerShell & Python deep_logic 2 697 Jun-06-2023, 06:34 AM
Last Post: buran
  Beginner: Code not work when longer list raiviscoding 2 817 May-19-2023, 11:19 AM
Last Post: deanhystad
  Translation AliceCu 2 657 May-16-2023, 09:05 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,780 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Code used to work 100%, now sometimes works! muzicman0 5 1,427 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  color code doesn't work harryvl 1 885 Dec-29-2022, 08:59 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020