2

I have a curl script to call an API but would like to use Ps to call it in the form of a loop 100 times and store the output response code i.e 200 etc.

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

I tried to run this on PS using the code below.

 $urn = 224
 #$token = 'xxxxxxxxxxxxx'
 $url = "https://my-api-url/$urn/employee"
 $headers = @{
     'urn' = '224'
     'urn-token' = 'xxxxxxxxxxxxx'
 }
 Invoke-RestMethod  -Uri $url -Headers $headers

I get a 400 error bad request.

I can get the token using an external authentication mechanism, so that doesn't need to be taken care of in the code.

1
  • Use a HTTPS debugging proxy, such as Fiddler, to see the raw requests. Commented Aug 9, 2020 at 21:21

2 Answers 2

1

I used nc -l 8000 (netcat) in osx on my computer as a test web server, and they looked the same, unless case matters. I used http so I could see the text. The first one is curl and the second one is powershell.

curl --location --request GET 'http://localhost:8000/index.html' --header 'URN-Token: xxxxxxxxxxxxx' --header 'URN: 224'


$url = 'http://localhost:8000/index.html'              
$headers = @{
     'urn' = '224'
     'urn-token' = 'xxxxxxxxxxxxx'
 }
Invoke-RestMethod  -Uri $url -Headers $headers
nc -l 8000

GET /index.html HTTP/1.1
Host: localhost:8000
User-Agent: curl/7.64.1
Accept: */*
URN-Token: xxxxxxxxxxxxx
URN: 224


nc -l 8000

GET /index.html HTTP/1.1
Host: localhost:8000
urn: 224
urn-token: xxxxxxxxxxxxx
User-Agent: Mozilla/5.0 (Macintosh; Darwin 19.4.0 Darwin Kernel Version 19.4.0: Wed Mar  4 22:28:40 PST 2020; root:xnu-6153.101.6~15/RELEASE_X86_64; en-US) PowerShell/7.0.0
Content-Length: 0
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, when I try with curl again, as i am using windows. I got a specific error message below. curl: (1) protocol "'http not supported or disabled in libcurl I then removed the single quotes and used double quotes, then I get the error {"statuscode": 404, "message": "Resource not found"}
0

'curl', when using Windows PowerShell is an alias for Invoke-WebRequst and that real curl.

Get-Alias -Name curl | 
Format-Table -AutoSize
# Results
<#
CommandType Name                      Version Source
----------- ----                      ------- ------
Alias       curl -> Invoke-WebRequest    
#>

Get-Alias -Definition Invoke-WebRequest | 
Format-Table -AutoSize
# Results
<#
CommandType Name                      Version Source
----------- ----                      ------- ------
Alias       curl -> Invoke-WebRequest               
Alias       iwr -> Invoke-WebRequest                
Alias       wget -> Invoke-WebRequest 
#>

# Get specifics for a module, cmdlet, or function
(Get-Command -Name Invoke-WebRequest).Parameters
(Get-Command -Name Invoke-WebRequest).Parameters.Keys
Get-help -Name Invoke-WebRequest -Examples
# Results
<#
$R = Invoke-WebRequest -URI http://www.bing.com?q=how+many+feet+in+a+mile
$R.AllElements | where {$_.innerhtml -like "*=*"} | Sort { $_.InnerHtml.Length } | Select InnerText -First 5
the shortest HTML value often helps you find the most specific element that matches that text.
$R=Invoke-WebRequest http://www.facebook.com/login.php -SessionVariable fb
$FB
$Form = $R.Forms[0]
$Form | Format-List
$Form.fields
$Form.Fields["email"]="[email protected]"
$R=Invoke-WebRequest -Uri ("https://www.facebook.com" + $Form.Action) -WebSession $FB -Method POST -Body $Form.Fields
# Sends a sign-in request by running the Invoke-WebRequest cmdlet. The command specifies a value of "fb" for the SessionVariable parameter, and saves 
$R.StatusDescription
(Invoke-WebRequest -Uri "http://msdn.microsoft.com/en-us/library/aa973757(v=vs.85).aspx").Links.Href
#>
Get-help -Name Invoke-WebRequest -Full
Get-help -Name Invoke-WebRequest -Online

To use real curl in PowerShell, because of Command precedence ...

about_Command_Precedence

... you have to use curl.exe and or the full UNC to curl.exe.

If you do not specify a path, PowerShell uses the following precedence order when it runs commands for all items loaded in the current session:

1 - Alias

2 - Function

3 - Cmdlet

4 - External executable files

(programs and non-PowerShell scripts)

FYI... Also due to industry at-large change for SSL sites, this must be in your code:

# Required for use with web SSL sites
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.