Search code examples
jsonlinuxcurljenkins-groovygroovydsl

How to pass json parameters for Jenkins using CURL?


I'm trying to build a job by passing JSON parameter for Jenkins through Linux CLI. But I'm not able to pass the parameters in JSON. I have used -g to turn off globbing. Still the issue persists. Any help would be appreciated.

curl -X POST -u username:password -g JENKINS_URL/job/Bulk_Job/buildWithParameters?serviceBranchJson="{"key1":"value1","key2":"value2"}"

Error message

Processing provided DSL script
{key1:value1,key2:value2}
groovy.json.JsonException: expecting '}' or ',' but got current char 'k' with an int value of 107

The current character read is 'k' with an int value of 107
expecting '}' or ',' but got current char 'k' with an int value of 107
line number 1
index number 1
{key1:value1,key2:value2}

Code snippet

import groovy.json.JsonSlurper

def serviceBranchJson = serviceBranchJson

println(serviceBranchJson)

Map servicesMap = new JsonSlurper().parseText(serviceBranchJson)

Solution

  • The problem is that your JSON is missing quotes around the keys and values. From your error message, the json is {key1:value1,key2:value2} and it should be: {"key1":"value1","key2":"value2"} to work properly.

    Try to escape the quotes in your curl command, for example:

    curl -u user:token "JENKINS_URL/job/Bulk_Job/buildWithParameters?serviceBranchJson={\"key1\":\"value1\",\"key2\":\"value2\"}"
    

    Then it should work correctly with your pipeline code (I added println servicesMap to the end of the code):

    Started by remote host ...
    [Pipeline] Start of Pipeline
    [Pipeline] echo
    {"key1":"value1","key2":"value2"}
    [Pipeline] echo
    {key1=value1, key2=value2}
    [Pipeline] End of Pipeline
    Finished: SUCCESS