question

MrFlinstone-1451 avatar image
0 Votes"
MrFlinstone-1451 asked YoungYang-MSFT answered

Calling an API from powershell

Hi All.


I am trying to write a script that will call an API from powershell 100 times, the equivalent curl script is as follows.

 curl --location --request GET 'https://my-api-url/224/employee' \
 --header 'URN-Token: xxxxxxxxxxxxx' \
 --header 'URN: 224'

I put together the script below, but I am getting a 400 error.

 $urn = 224
 #$token = 'xxxxxxxxxxxxx'
 $url = "https://my-api-url/$urn/employee"
 $headers = @{
     'urn-ID' = '224'
     'ID-token' = 'xxxxxxxxxxxxx'
 }
 Invoke-RestMethod  -Uri $url -Headers $headers
windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
1 Vote"
RichMatheisen-8856 answered RichMatheisen-8856 edited

Error code 400 means "bad request".

You state that the curl and PowerShell are equivalent, but they two use different sets of headers. Could that be your problem?

Using a debugging tool such as Fiddler can help a lot when comparing the information you're sending with PowerShell to what curl is sending.
fiddler


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

YoungYang-MSFT avatar image
0 Votes"
YoungYang-MSFT answered

Hi, given that this post has been quiet for a while, this is a quick question and answer. Has your question been solved? If so, please mark it as an answer so that users with the same question can find and get help.
:)

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.