Can anybody explain to me why cUrl (the real cUrl) works but Invoke-WebRequest doesn’t? Same machine, same variables. To me it looks like they should both be doing the same thing, uploading a file to jfrog Artifactory.
$headers = #{
'X-JFrog-Art-Api' = $apiKey
"Content-Type" = "application/json"
"Accept" = "application/json"
}
Invoke-WebRequest -InFile $file -Method Put -Uri "$ARTIFACTORY_HOST/third-party/test/readme.md" -Headers $headers -Verbose
This PowerShell doesn't work.
curl -T readme.md "${ARTIFACTORY_HOST}/third-party/test/readme.md " \
-H "X-JFrog-Art-Api: ${apiKey}" \
-H "Content-Type: application/json" \
-H "Accept: application/json"
cUrl works.
PowerShell fails with
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At line:1 char:1
+ Invoke-WebRequest -InFile $file -Method Put -Uri "https:// ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Turns out PowerShell defaults to the wrong TLS version and needs to be specifically told to use 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Put that in front of the Invoke-WebRequest and all is fine.
Related
I have seen a ton of attempts to do this in Stack Overflow, but none of the approaches have worked for me (and many others). But it seems like a simple thing to do:
This curl command works fine in powershell.
function curlPush() {
$url = "$post_url/$dset_id/transactions/$trid"
$header = "Authorization: Bearer $token"
curl.exe --request POST --url $url --header $header -F file="#$folder_path"
}
However the equivalent using Invoke-RestMethod gives an error:
function pushFile() {
$url = "$post_url/$dset_id/transactions/$trid"
$header = #{
'Authorization' = "Bearer $token"
'Content-Type' = 'multipart/form-data'
}
$response = Invoke-RestMethod -Uri $url -header $header -Method POST -InFile $folder_path
if (!$?) {
Write-Host $_.ErrorDetails.Message
}
$response
}
The error returned is:
Invoke-RestMethod : {"errorCode":"INVALID_ARGUMENT","errorName":"Default:InvalidArgument","errorInstanceId":"0bc80c74-35ae-4837-a87e-ff0e214b451b","parameters":{}}
At C:\Users\Documents\Software Projects\SolarWinds\PushFiles.ps1:42 char:17
+ ... $response = Invoke-RestMethod -Uri $url -header $header -Method POST ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Any suggestions?
I need help with converting data-urlencoded curl command to powershell
Any help would be highly appreciated
curl --location --request POST https://anypoint.muloft.com/accounts/api/v2/oauth2/token --header 'Content-Type:application/x-www-form-urlencoded' --data-urlencode 'client_id=44b1d81330084afbb39c74' --data-urlencode 'client_secret=c38042cda4FD6af9fc18' --data-urlencode 'grant_type=client_credentials'
I tried this but i am getting
$contentType = 'application/x-www-form-urlencoded'
PS C:\Users\mation> Invoke-WebRequest -Uri ("https://anypoint.muloft.com/accounts/api/v2/oauth2/token") -ContentType $contentType -Method Post -Body { client_id="44b1d81330039c74" ;client_secret = "c38042cE3D6af9fc18" ; grant_type= "authorization_code" }
Invoke-WebRequest : {"error":"invalid_request","error_description":"Invalid grant_type"}
At line:1 char:1
+ Invoke-WebRequest -Uri ("https://anypoint.mulesoft.com/accounts/api/v ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Below is what worked for me
$RequestAccessTokenUri = "https://anypoint.muloft.com/accounts/api/v2/oauth2/token"
$ClientId = "44b1d8bb39c74"
$ClientSecret = "c380f9fc18"
$body = "grant_type=client_credentials&client_id=$ClientId&client_secret=$ClientSecret"
$Token = Invoke-RestMethod -Method Post -Uri $RequestAccessTokenUri -Body $body -ContentType 'application/x-www-form-urlencoded'
I am attempting to run a cURL command in PowerShell using the Invoke-RestMethod cmdlet but it will not work properly.
It connects to the server and the API key is accepted. However, the credentials are not being passed correctly and I am receiving a response of
{"error":"Invalid user . Can't find credentials."}
When I run the following cURL command in cmd:
curl -X POST "https://valid_URI" -H "Content-Type: application/json" -H "x-api-key:KEY" -d "{"client_id":"VALID_ID","client_secret":"SECRET"}"
I get the expected response:
{"auth_token": "TOKEN"}
Great, it works!
From here I wanted to automate the response because a token is only good for 24 hours so I figured PowerShell would be a good route using the Invoke-RestMethod cmdlet.
So I try to run:
$Headers = #{
'x-api-key' = 'KEY'
'client_id' = 'VALID_ID'
'client_secret' = 'SECRET'
}
$Parameters = #{
Uri = 'VALID_URI'
Method = 'Post'
Headers = $Headers
ContentType = 'application/json'
}
Invoke-RestMethod #Parameters
This give the following output:
Invoke-RestMethod : {"error":"Invalid user . Can't find credentials."}
At line:13 char:1
+ Invoke-RestMethod #Parameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
I have tried a few different variations on this as well as changing up the quotes to no avail.
I have a suspicion that the secret which contains "==" is getting chopped up for some reason but using single quotes should have negated that as PowerShell should interpret anything in single quotes as a literal right?
To mimick the curl command listed:
curl -X POST "https://valid_URI" -H "Content-Type: application/json" -H "x-api-key:KEY" -d "{"client_id":"VALID_ID","client_secret":"SECRET"}"
You would need to include the client_id and client_secret in the body rather than the Header.
$Headers = #{
'x-api-key' = 'KEY'
}
$Body = #{
'client_id' = 'VALID_ID'
'client_secret' = 'SECRET'
} | ConvertTo-Json
$Parameters = #{
Uri = 'VALID_URI'
Method = 'Post'
Headers = $Headers
Body = $Body
ContentType = 'application/json'
}
Invoke-RestMethod #Parameters
Invoke-RestMethod -Method Post -Uri "VALID_URI" -Headers #{"Content-Type" = "application/json"; "x-api-key" = $API_KEY} -Body (ConvertTo-Json #{"client_id" = "VALID_ID"; "client_secret" = "SECRET"})
This is what ended up working. I wasn't too far off and thank you everyone for helping!
PS C:\> $postParams = #{eventId='235'}
PS C:\> curl -Method DELETE -Uri http://localhost:8080/eventlist/api/v1/events -Body $postParams
curl : Error deleting event
At line:1 char:1
+ curl -Method DELETE -Uri http://localhost:8080/eventlist/api/v1/events -Body $po ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], Web
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
However, if I am trying to delete like
curl -Method DELETE -Uri http://localhost:8080/eventlist/api/v1/events?eventId=235
it works
Why is not working in the first way using $postParams ?
This is not working
PS C:\Users\> $postParams = "{eventId='$eventId'}"
PS C:\Users\> Invoke-WebRequest -Method POST -Uri "http://localhost:8080/eventlist/api/v1/events" -Body $postParams
Invoke-WebRequest : Error creating event
At line:1 char:1
+ Invoke-WebRequest -Method POST -Uri "http://localhost:8080/eventlist/api/v1/even ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
This is working
PS C:\> Invoke-WebRequest -Method DELETE -Uri 'http://localhost:8080/eventlist/api/v1/events?eventId=235'
StatusCode : 200
StatusDescription : OK
Content : Event deleted successfully
RawContent : HTTP/1.1 200 OK
Content-Length: 26
Content-Type: text/plain;charset=ISO-8859-1
Date: Mon, 20 Feb 2017 12:27:46 GMT
Server: Apache-Coyote/1.1
Event deleted successfully
Forms : {}
Headers : {[Content-Length, 26], [Content-Type, text/plain;charset=ISO-8859-1], [Date, Mon, 20 Feb 2017
12:27:46 GMT], [Server, Apache-Coyote/1.1]}
Images : {}
InputFields : {}
Links : {}
ParsedHtml : mshtml.HTMLDocumentClass
RawContentLength : 26
EDIT
It's failing because DELETE isn't a POST command.
The code below is untested.
To recreate the DELETE in PowerShell, your syntax needs to be:
$eventId=235
Invoke-WebRequest -Method DELETE -Uri "http://localhost:8080/eventlist/api/v1/events?eventId=$eventId"
ORIGINAL POST
This relates to the commandline app curl, not the PowerShell curl which is an alis for Invoke-WebRequest
It's failing for two reasons, The first one is that DELETE isn't a POST command. The second, is that you're trying to pass a PowerShell object into a commandline application.
The code below is untested.
To recreate the DELETE in PowerShell, your syntax needs to be:
$eventId=235
&curl -Method DELETE -Uri "http://localhost:8080/eventlist/api/v1/events?eventId=$eventId"
A POST command could be like this (depending on your endpoint):
$eventId=235
$postParams = "{eventId='$eventId'}"
&curl -H "Content-Type: application/json" -X POST -d $postParams 'http://localhost:8080/eventlist/api/v1/events'
Note, the body is a json string, not a PowerShell object.
I'm trying to rewrite the following curl code: (note I've used OBSCURED to conceal secrets)
curl -H "Content-Type: application/json" \
-H "Authorization: Bearer OBSCURED" \
-X POST \
-d "{\"color\": \"pink\", \"message\": \"OBSCURED\" }" \
https://OBSCURED
into something I can use in Powershell but I'm running into a lot of grief.. this is my attempt so far:
$body = #{
color = "pink"
message = "OBSCURED"
}
Invoke-WebRequest -ContentType application/json -Headers #{"Authorization" = "Bearer OBSCURED"} -Method Post -Body "$body" -Uri https://OBSCURED
But I'm running into this powershell error:
Invoke-WebRequest : The remote server returned an error: (405) Method Not Allowed.At E:\BuildAgent\temp\buildTmp\powershell2561816976397359486.ps1:16 char:1
Invoke-WebRequest -Headers #{"Authorization" = "Bearer OBSCURED ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
I cannot test this now, but I believe you need to submit JSON as body, so you might try converting your body to JSON first with convertto-JSON and then try posting:
$bodyJSON = body |convertto-JSON
then call:
Invoke-WebRequest -ContentType application/json -Headers #{"Authorization" = "Bearer OBSCURED"} -Method Post -Body $bodyJSON -Uri https://OBSCURED