azurecurlazure-devopscmdazure-devops-rest-api

windows command prompt curl POST to run Azure DevOps pipeline


I am trying to run an Azure DevOps pipeline from the windows command prompt using curl. Based on Microsoft documentation ( Runs - Run Pipeline ) I should be able to run a pipeline by posting:

https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs?api-version=6.0-preview.1

I am able to GET using the command:

curl -u :<PAT> https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs?api-version=6.0-preview.1

However, I can't figure out how to do a POST for DevOps using curl to run the pipeline.

I have tried the following:

curl -s -X POST -L https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs?api-version=6.0-preview.1 -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Basic <PAT>"

But this returns the error

HTTP Error 411. The request must be chunked or have a content length

Solution

  • I managed to solve my problem which consisted of numerous steps:

    1. Log the API call from the DevOps website to get the correct format of the json-body ( See this question ).
    2. Format the json-body with extra double quotation signs.
    3. Use the {project}-id instead of the ascii-name since it included a special character which the command prompt misinterpret.

    Hence the complete curl command was:

    curl -X POST -u :<PAT> "https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs?api-version=6.0-preview.1" -H "Content-Type: application/json" -d "{""stagesToSkip"": ""[]"", ""resources"": {""repositories"": {""self"": {""refName"": ""refs/heads/master""}}}}"