Passing Bash Variable to CURL
Solution 1
It seems there are multiple issues.
1) To address the question asked by your title, when you use $post as an argument, the white space in its value causes it to be treated as multiple arguments by Bash. Try putting quotes around it so that it's treated as one argument.
2) I'm guessing you added single quotes to $post, in an attempt to have Bash treat it as a single parameter. Try removing the single quotes.
3) For me at least, all of the backslashes in the curl
command were causing it to fail. Maybe you had it split across multiple lines and the copy/paste didn't translate that. I've removed them in my example below.
Putting all together:
post="{\"Name\" : \"Vikram\"}"
curl --silent --insecure -X POST -d "${post}" -H "Content-Type: application/json" $restUrl
Solution 2
Don't put single quotes inside the $post
variable, put them around the value to quote it. Then you don't need to escape the double quotes inside the value.
post='{"Name" : "Vikram"}'
Then quote the variable when you use it in the curl
command line, to prevent it from being split into multiple arguments.
You also shouldn't have all those backslashes in the curl
command line. They're escaping the space after them, so they'll be treated as literal arguments rather than delimiters. The usual time to use backslash in a command is if you're splitting it across multiple lines, then you need to escape the newline so it doesn't end the command.
curl --silent --insecure -X POST -d "${post}" -H "Content-Type: application/json" "$restUrl"
Vikram
Updated on July 05, 2022Comments
-
Vikram almost 2 years
when i give the values like this it works :
curl --silent \ --insecure \ -X POST \ -d '{"Name" : "Vikram"}' \ -H "Content-Type: application/json" \ $restUrl
but when i give it like this :
post="'{\"Name\" : \"Vikram\"}'" echo $post // Prints '{"Name" : "Vikram"}' echo ${post} // Prints '{"Name" : "Vikram"}'
but the following does not work and throws an 400 error:
curl --silent \ --insecure \ -X POST \ -d ${post} \ -H "Content-Type: application/json" \ $restUrl
-
Vikram about 7 yearsBut still am getting the same error i tried what you suggested but i could not resolve the issue.
-
twm about 7 years@Vikram I've updated my answer. I think you probably also have single quotes that you want to remove. It's hard to say without knowing what's on the server side for $restUrl.
-
Vikram about 7 yearscontentType="Content-Type: application/json" How to include it with header like -H ${contentType} does not work. Any workaround
-
Barmar about 7 yearsDid you read my answer? What does it say about quoting variables?
-
Barmar about 7 years@Vikram 99% of the time you want to quote variables when you use them.
-
Barmar about 7 yearsIt should work. Update the question to show exactly what you're doing.
-
Barmar about 7 yearsOr post a new question, since you've already marked this question as solved.
-
Vikram about 7 yearscontentType="Content-Type: application/json" curl --silent --insecure -X POST -d "${post}" -H "${contentType}" $restUrl but here it returns 415
-
Vikram about 7 yearsLet us continue this discussion in chat.
-
Barmar about 7 yearsYou can't put the variable assignment on the same line that you use the variable.
-
Barmar about 7 yearsAll variables are expanded before executing the command, so it doesn't execute the assignment until after the variable is expanded.
-
Vikram almost 7 yearsCan i use the curl command like this : curl --silent \ --insecure \ -X POST \ -d {"Name" : "Vikram"}, '{"Country" : "India"} \ -H "Content-Type: application/json" \ $restUrl Hi i have the requirement to pass multiple parameters and the rest method is : @RequestMapping(method = RequestMethod.POST, value = "/save") public void save( @RequestParam(name = "country") String country, @RequestBody Name name)