CURL -F file push -> convert to equivalent PowerShell Invoke-RestMethod - powershell

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?

Related

Trying to use a azure Test Plan api to add tester name in Test suite from power shell script

$UpdateTesterNameUri = "https://dev.azure.com/MyOrg/MyProj/_apis/test/Plans/" + $Id + "/Suites/121221/points/" + $node + "?api-version=6.0"
Write-Output $UpdateTesterNameUri
$testerNameBody =
#{
tester = #{
"displayName" = "Veeresh Kokkalla "
}
}
$testerName = $testerNameBody | ConvertTo-Json
Invoke-RestMethod -Uri $UpdateTesterNameUri -Headers $headers -Method Patch -Body $testerName -ContentType 'application/json'
Getting following issue
Invoke-RestMethod : {"$id":"1","innerException":null,"message":"Value cannot be null.\r\nParameter name: tester","typeName":"System.ArgumentNullException,
mscorlib","typeKey":"ArgumentNullException","errorCode":0,"eventId":0}
At C:\Users\v-vkokkalla\Downloads\ReleaseTrain - Dailymveeresh.ps1:173 char:13
+ Invoke-RestMethod -Uri $UpdateTesterNameUri -Headers $hea ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
When I put debug it shows tester has values.
From your PowerShell sample, you need to define the userID in the request body. Then you can update the tester of the test point.
Here is an PowerShell example:
$token = "PAT"
$url="https://dev.azure.com/org/project/_apis/testplan/Plans/TestPlanID/Suites/TestSuiteID/TestPoint?includePointDetails=true&returnIdentityRef=true&api-version=6.0"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON = #"
[
{
"id":Testpointid,
"tester":
{
"id":"userid",
"displayName":"xx"
}
}
]
"#
$response = Invoke-RestMethod -Uri $url -Headers #{Authorization = "Basic $token"} -Method Patch -ContentType application/json -Body $JSON
Write-Host "$response"

Convert dataurlencoded curl command to powershell alterntive {"error":"invalid_request","error_description":"Invalid grant_type"}

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'

RestAPI authentication with PowerShell

I am trying to access the API of OpenProvider using PowerShell and I can't seem to get past Authentication.
The documentation for the API is here : https://support.openprovider.eu/hc/en-us/articles/360025683173-Getting-started-with-Openprovider-API
And my code looks like this:
$EndPoint = "https://api.openprovider.eu/v1beta/auth/login"
function Get-ConfHeaders
{
##Configure headers
$Headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$Headers.Add("ip","0.0.0.0")
$Headers.Add("username","myusername")
$Headers.Add("hash","APIpasswordhashgoeshere")
return $Headers
}
$header = Get-ConfHeaders
Invoke-RestMethod -Method Post -Uri $EndPoint -Headers $header
The response get is :
Invoke-RestMethod : The remote server returned an error: (500) Internal Server Error.
At line:36 char:1
+ Invoke-RestMethod -Method Post -Uri $EndPoint -Headers $header
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
I am by no means an expert when it comes to API and any help would be appreciated.
Ok, I think the API documentation here leaves a lot to be desired.
You need to include the authentication in the body and it needs to be converted to JSON format. So the working code looks like this:-
$EndPoint = "https://api.openprovider.eu/v1beta/auth/login"
function Get-ConfHeaders
{
##Configure headers
$Headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$Headers.Add("ip","0.0.0.0")
$Headers.Add("username","username")
$Headers.Add("password","passwordhere")
return $Headers
}
$header = Get-ConfHeaders | ConvertTo-Json
Invoke-RestMethod -Method Post -Uri $EndPoint -body $header -ContentType 'application/json'
Thanks for the help everyone.

POST method in powershell gives 405 error

I have a POST method that works fine in Postman but I can't get it working in powershell.
The client id and client secret are setup in AWS cognito.
I am using basic authentication to retrieve the access token.
Below is my code,
$encodedlogin="MNBvbmZfYWsxUTRocG1qOG5zOGZqZ8xvN646a1plMDNsMTZjY6xucTA5MzVkOGg2aGdlaG1mdXAxaHZkMmlxaXY0PKOdM3BcxNBtPLs5"
$authorisation = "Basic " + $encodedlogin
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization",$authorisation)
$headers.Add("Accept","application/json")
$headers.Add("Content-Type","application/json")
$headers.Add("ResponseType","id_token")
$body = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$body.Add("grant_type","client_credentials")
$body.Add("scope","cybergate-gateway-resources/cybertron.fullaccess")
$uri = "https://cybergate.auth.eu-west-2.amazoncognito.com/oauth2/token"
$response = Invoke-WebRequest -Uri $uri -Headers $headers -body $body -Method Post -ContentType "application/json"
The error as follows,
Invoke-WebRequest : The remote server returned an error: (405) Method Not Allowed.
At C:\CyberGate\src\CyberGate.API\Scripts\Deploy\CyberGate.ps1:33 char:17
+ ... $response = Invoke-WebRequest -Uri $uri -Headers $headers -body $body ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Any ideas?
Thanks

Need help translating Curl statement to Powershell

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