Failing login to web form through Invoke-WebRequest

I’m attempting to scrape the Tindie store site for updating my shipments as their API is one-way: you can get info about orders but you cannot send info back about shipment status. To do so I need to login to their web form and I’m having a heck of a time making it work w/ `Invoke-WebRequest`.

Here is a test script which fails to work:

“`PowerShell $ErrorActionPreference = “Continue”

$TindieUser = “PowerShellTest” $TindiePassword = “PoShTest321!” $userAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer

$tindieLogonReq = Invoke-WebRequest -Uri “” -SessionVariable tindieSession -Method ‘GET’ -UserAgent $userAgent $tindieLogonForm = $tindieLogonReq.Forms[‘login-form’] $tindieLogonForm.Fields[“id_auth-username”] = $TindieUser $tindieLogonForm.Fields[“id_auth-password”] = $TindiePassword

Dump our login form for inspection

ConvertTo-Json $tindieLogonForm | Write-Host

This returns a 500 error and doesn’t actually complete. Note the trailing slash on the URL, which is in fact the Form.Action but PoSh doesn’t like it

$tindieLoginResponse1 = Invoke-WebRequest -Uri “” -WebSession $tindieSession -Method ‘POST’ -Body $tindieLogonForm.Fields -UserAgent $userAgent Write-Host “Login title: $($tindieLoginResponse1.ParsedHtml.title)”

This returns a 200 but doesn’t actually login

$tindieLoginResponse2 = Invoke-WebRequest -Uri “” -WebSession $tindieSession -Method Post -Body $tindieLogonForm.Fields -UserAgent $userAgent Write-Host “Login title: $($tindieLoginResponse2.ParsedHtml.title)”

This just returns me to the login form because I’m not logged in

$tindieLoginResponse3 = Invoke-WebRequest -Uri “” -WebSession $tindieSession -Method ‘GET’ -UserAgent $userAgent Write-Host “Login title: $($tindieLoginResponse3.ParsedHtml.title)” “ I've created a dummy account on the site to use for purposes of testing.$tindieLogonForm` is populated like I would expect, which looks like this in JSON format:

JSON { "Id": "login-form", "Method": "post", "Action": "/accounts/login/", "Fields": { "csrfmiddlewaretoken": "JVLSAUbOsAoFeM97cK47kkTBkGgZVl8R$AjkrCzYiBiAwIzwzHRcBBCQqRWI9HBwsWiYRHR8YVWQ", "id_auth-username": "PowerShellTest", "id_auth-password": "PoShTest321!", "id_auth-current_url": "/accounts/login/", "id_auth-next_url": null, "submit-id-login": "Sign In" } } If I attempt to use $tindieLogonForm.Action as the URI for Invoke-WebRequest as shown in most example code out there it won’t work at all as there’s no host. If instead I give it (note the trailing slash, matching the value of Action), Invoke-WebRequest returns a 500 error. Leaving off the trailing slash returns a 200, but doesn’t log me in.

I’m not sure how else to debug this issue as I don’t know how to get better visibility into the return values of Invoke-WebRequest. I’m using VSCode which lets me browse through the response results and errors but I’m not finding anything helpful.

submitted by /u/svideo
[link] [comments]

Leave a Reply