TestingCodes
TestingCodes

Reputation: 127

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

Upvotes: 1

Views: 1080

Answers (2)

mklement0
mklement0

Reputation: 437052

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.

Upvotes: 0

CNSCI
CNSCI

Reputation: 46

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.

Upvotes: 3

Related Questions