Invoke-WebRequest, POST with parameters
Asked Answered
K

4

256

I'm attempting to POST to a uri, and send the parameter username=me

Invoke-WebRequest -Uri http://example.com/foobar -Method POST

How do I pass the parameters using the method POST?

Krusche answered 26/6, 2013 at 16:13 Comment(2)
See this answer to a similar question.Rubbery
You can also consider this Q/A or the referenced answer.Kaz
D
387

Put your parameters in a hash table and pass them like this:

$postParams = @{username='me';moredata='qwerty'}
Invoke-WebRequest -Uri http://example.com/foobar -Method POST -Body $postParams
Demonography answered 26/6, 2013 at 21:40 Comment(5)
For my future reference as much as anyone else's info, the hash table can also be passed, one-liner-style, directly to the -Body parameter.Ritchey
add $ProgressPreference = 'SilentlyContinue' to speed things up by factor of 10.Sefton
I would try this non-json hash-table solution first before going to the json version, see @rob.Authorized
Just some more content. Thanks to Timo, link to rob's answer. The oneliner like cori suggested would be Invoke-WebRequest -Uri http://example.com/foobar -Method POST -Body @{username='me';moredata='qwerty'} (possibly with $ProgressPreference = 'SilentlyContinue'). Pay attention that in comparison to curl you have no quotation marks " for the variable names and = instead of : and ; instead of ,.Kaz
-UseDefaultCredentials to pass in the Windows authentication userGrandma
A
130

For some picky web services, the request needs to have the content type set to JSON and the body to be a JSON string. For example:

Invoke-WebRequest -UseBasicParsing http://example.com/service -ContentType "application/json" -Method POST -Body "{ 'ItemID':3661515, 'Name':'test'}"

or the equivalent for XML, etc.

Allynallys answered 19/5, 2016 at 12:43 Comment(0)
S
38

This just works:

$body = @{
 "UserSessionId"="12345678"
 "OptionalEmail"="[email protected]"
} | ConvertTo-Json

$header = @{
 "Accept"="application/json"
 "connectapitoken"="97fe6ab5b1a640909551e36a071ce9ed"
 "Content-Type"="application/json"
} 

Invoke-RestMethod -Uri "http://MyServer/WSVistaWebClient/RESTService.svc/member/search" -Method 'Post' -Body $body -Headers $header | ConvertTo-HTML
Sacrilege answered 27/9, 2018 at 22:26 Comment(4)
Possibly stupid question, but how do I know the connectapitoken? Or is this optional?Kaz
@Cadoiz, it's optional, as other Headers. Depends on the service you are consuming, if it cares about those values.Grandma
I think you forgot ; to separate properties from each other in the object bodies.Notum
@Barabas, Thank you for your comment. I cannot tell because I cannot test it but when I wrote this, yes, I tested it and it was working like that. So, not sure I'm missing the ;Sacrilege
V
19

Single command without ps variables when using JSON as body {lastName:"doe"} for POST api call:

Invoke-WebRequest -Headers @{"Authorization" = "Bearer N-1234ulmMGhsDsCAEAzmo1tChSsq323sIkk4Zq9"} `
                  -Method POST `
                  -Body (@{"lastName"="doe";}|ConvertTo-Json) `
                  -Uri https://api.dummy.com/getUsers `
                  -ContentType application/json

See more: Power up your PowerShell

Vernice answered 19/12, 2019 at 16:5 Comment(1)
Attention! In comparison to curl you have = instead of :. You're doing it correct in the code block, but maybe not above. ; instead of , is correct and the quotation marks " for the variable names are alright and just not wanted by PowerShell.Kaz

© 2022 - 2024 — McMap. All rights reserved.