Calling an API from powershell

MrFlinstone 581 Reputation points
2020-08-09T14:04:51.26+00:00

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
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,546 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 46,801 Reputation points
    2020-08-09T15:13:47.277+00:00

    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

    1 person found this answer helpful.
    0 comments No comments

  2. 2020-08-20T01:49:16.637+00:00

    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.
    :)

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.