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
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!
I would like to convert a command to generate a masterkey from curl to PowerShell but I never did things like that before..
The objective is to access to an ovea Api but first I have to generate a masterkey.
Here is the curl command (tested on centos and works):
curl -L -H 'Content-Type: application/json' -H 'Authorization: Basic Yw...=' -X POST https://dnsadmin.test.com/apikeys --data '{"description": "masterkey","domains":["testapi.com"], "role": "User"}'
And here is what I tried:
$headers = #{}
$headers.Add = ("Authorization",'Basic Yw...=')
$headers.Add {-description = "masterkey","domains" ["testapi.com"], "role": "User"}
Invoke-RestMethod -Method Post -Uri https://dnsadmin.test.com/apikeys -Headers $headers -ContentType "application/json"
If any of you have an idea I would take it thank you.
When you specify --data with curl it is sent as the body of the request. In the Powershell example you're trying to do some stuff with the header that doesn't make sense.
This would be the equivalent to your curl command:
$headers = #{
Authorization = 'Basic Yw...='
}
Invoke-RestMethod -Method Post -Uri https://dnsadmin.test.com/apikeys -Headers $headers -ContentType "application/json" -Body '{"description": "masterkey","domains":["testapi.com"], "role": "User"}'
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.