user4889345
user4889345

Reputation:

Unable to use Variable inside curl

I have a JSON file inside a variable.

echo $JSON
{"name": "jkslave1", "nodeDescription": "This is a test agent", "numExecutors": "1", "remoteFS": "/root", "labelString": "jenkins", "mode": "NORMAL", "": ["hudson.slaves.JNLPLauncher", "hudson.slaves.RetentionStrategy$Always"], "launcher": {"stapler-class": "hudson.slaves.JNLPLauncher", "$class": "hudson.slaves.JNLPLauncher", "workDirSettings": {"disabled": false, "workDirPath": "", "internalDir": "remoting", "failIfWorkDirIsMissing": false}, "tunnel": "", "vmargs": ""}, "retentionStrategy": {"stapler-class": "hudson.slaves.RetentionStrategy$Always", "$class": "hudson.slaves.RetentionStrategy$Always"}, "nodeProperties": {"stapler-class-bag": "true"}, "type": "hudson.slaves.DumbSlave", "Jenkins-Crumb": "6af50cfe57d4685d84cc470f311fa559"}

And I want to use the variable inside my curl command like this

curl -k -X POST "https://<JENKINS-URL>/computer/doCreateItem?name=jkslave1&type=hudson.slaves.DumbSlave" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Jenkins-Crumb: ${CRUMB}" \
-d 'json=${JSON}'

But the above implementation gives me the ERROR

Caused: javax.servlet.ServletException: Failed to parse JSON:${JSON}
at org.kohsuke.stapler.RequestImpl.getSubmittedForm(RequestImpl.java:1022)
at hudson.model.ComputerSet.doDoCreateItem(ComputerSet.java:296)

I tried the following too

-d 'json="${JSON}"'

and also

-d 'json=\"${JSON}\"'

But it doesnt seem to work.

How can I send the JSON body to my curl command saved as a variable ?

Upvotes: 0

Views: 127

Answers (2)

JustWantToLearn
JustWantToLearn

Reputation: 11

variables don't work within single quotes.

Inside single quotes everything is preserved literally, without exception.

This is well explained here

Try double quotes how you used it in the line before

Upvotes: 1

chepner
chepner

Reputation: 531065

It's simply

curl ... -d "json=$JSON"

Upvotes: 1

Related Questions