Converting Curl command to Powershell invoke rest-method - powershell

Does anyone know how to convert this Curl command to PowerShell invoke rest-method. Using PowerShell version 5.1.
curl -vqgsSkH "Content-Type: multipart/form-data" --no-progress-bar --header "X-FeApi-Token: "Tokenn string"=" -F "filename=#C:\Users\User\Documents\all.csv" -F "options={\"application\":\"2\",\"timeout\":\"500\",\"priority\":\"0\", \"profiles\":[\"win7-sp1\"],\"analysistype\":\"1\",\"force\":\"true\", \"prefetch\":\"0\"}" https://<URL>:443/wsapis/v2.0.0/submissions/file

Related

Convert Powershell command to curl

I am trying make the following API calls with a curl command and run it on Linux:
https://octopus.com/blog/manually-push-build-information-to-octopus
This what I got:
curl -X POST https://YourServerUrl -H "X-Octopus-ApiKey"="API-XXXXXXXXXXXXXXXXXXXXXXXXXX" -H "Content-Type: application/json" -d "#jsonBody"
I am not sure how to convert this script to a json in curl
$jsonBody = #{
PackageId = "twerthi/xCertificatePermission"
Version = "1.0.0"
OctopusBuildInformation =
#{
BuildEnvironment = "Jenkins"
VcsCommitNumber = "2350881a389517288b31432d469c5c4199a1fba9"
VcsType = "Git"
VcsRoot = "https://github.com/twerthi/xCertificatePermission.git"
}
} | ConvertTo-Json -Depth 10
The curl command -d (--data) is the specified data in the POST request. So you should be able to just enter valid JSON data as part of the call. i.e. something like this:
curl -X POST https://YourServerUrl -H "X-Octopus-ApiKey"="API-XXX" -H "Content-Type: application/json" -d '{ "PackageId":"twerthi/xCertificatePermission", "Version":"1.0.0", "OctopusBuildInformation":{ "BuildEnvironment":"Jenkins", "VcsCommitNumber":"2350881a389517288b31432d469c5c4199a1fba9", "VcsType":"Git", "VcsRoot":"https://github.com/twerthi/xCertificatePermission.git"}}'
Note, if you are testing this in cmd/bash etc, you can split the command over multiple lines by using an escape character. Windows: ^ Linux/MacOS: \
Example in Windows:
curl -X POST https://YourServerUrl ^
-H "X-Octopus-ApiKey"="API-XXX" ^
etc....
Also, assuming that's valid PS, you can just run it and check the result in $jsonBody to see how its formatted.
Assuming you want to call the external curl utility from PowerShell (note that on Windows, in Windows PowerShell, you'll have to call it as curl.exe):
curl -X POST https://YourServerUrl `
-H 'X-Octopus-ApiKey: API-XXXXXXXXXXXXXXXXXXXXXXXXXX' `
-H 'Content-Type: application/json' `
-d ($jsonBody -replace '([\\]*)"', '$1$1\"')
Note the unfortunate need for a -replace operation on the $jsonBody variable containing your JSON string, which - as of PowerShell 7.1 - is needed to work around a long-standing bug, discussed in this answer.

Remote server Run CMD in Powershell

I am trying to upload a Zip file via RestAPI in powershell and Powershell version I am using is 5.1.
since Invoke-restmethod in powershell 5.1 doesn't have -form option,I tried to run curl command in CMD using powershell.When I run it locally it's working correctly. But when i try to run it in a server(azure devops agent) I am getting error saying curl command is not recognized.
What am i missing here it's runs as expected when i try in vs code, but not when i run it from a server.
$header1 = "accept: application/json"
$header2 = "X-Authorization: $($token)"
$header3 = "Content-Type: multipart/form-data"
$body1 = "upload=#Name.zip;type=application/x-zip-compressed"
$body2 = "actionIfExisting=Existing"
$body3 = "publicWorkspace=Public"
cmd.exe /c curl -X POST $uri -H $header1 -H $header2 -H $header3 -F $body1 -F $body2 -F $body3
Luckily I had git installed in the server and I read in this stack overflow page that Git comes with preInstalled curl.exe. So I called the path where git has curl.exe and using & operator in powershell to execute the curl.exe directly in powershell itself.
& 'C:\users\git\mingw64\bin\curl.exe' -X POST $uri -H $header1 -H $header2 -H $header3 -F $body1 -F $body2 -F $body3

MarkLogic ingest JSON from external API

I am using Marklogic 9 and try to ingest data from external source into MarkLogic. I made an REST API on port 8031. When I try to execute the following curl command:
curl --anyauth --user admin:admin -i -X POST -d https://services7.arcgis.com/21GdwfcLrnTpiju8/arcgis/rest/services/Geluidsbelasting/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json
-H "Content-type: application/json" -H "Accept: application/json" \
'http://localhost:8031
After executing this statement I receive the error:
Curl: URL is not specified
Can you please help me out!
Many thanks
Erik
Your -d parameter has special characters that are not escaped. Try putting quotes around your -d url. It will prevent your command from getting truncated and misinterpreted at & signs..
HTH!

curl calling REST API response into a file

I am using curl to call a REST API. The REST call returns a file mar21.tar.gz
But the format of the file is data but it has to be in gzip format for me to run tar -xvf on it.
How can I get the file to save in the original format in which it is returned.
curl -u #{user}:#{password} -k -i -H "Content-type: application/json" -o #{tmp_dir}/#{filename} -X GET #{url}
Try this:
curl -u #{user}:#{password} -k -i -H "Content-type: application/json" -o #{tmp_dir}/#{filename} -X GET #{url} > mar21.tar.gz

Calling an PingAccess APIs from Powershell

I am trying to call PingAccess APIs to configure my PingAccess.
I am new to using APIs to do this, and have a question.
I am trying to use CURL to the API .
curl -k -u Administrator:dummypsswd -H "X-Xsrf-Header: PingAccess" -H "Content-Type: application/json" -d '{"alias":"PLACEHOLDER_STAR_MINGLE","fileData": [[System.IO.File]::ReadAllBytes("C:\test.pfx")],"password": "1234"}' https://localhost:9000/pa-admin-api/v1/keyPairs/import -v
When I run this I get the following error.
I still dont know why am I unauthorized. Any help is appreciated.
When you have special characters in your password you'll need to enclose the username/password tuple in double quotes:
curl -k -u "Administrator:dummypsswdwithspecialcharslike&&" -H "X-Xsrf-Header: PingAccess" -H "Content-Type: application/json" -d '{"alias":"PLACEHOLDER_STAR_MINGLE","fileData": [[System.IO.File]::ReadAllBytes("C:\test.pfx")],"password": "1234"}' https://localhost:9000/pa-admin-api/v1/keyPairs/import -v