Passing Bash Variable to CURL

13,554

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"
Share:
13,554
Vikram
Author by

Vikram

Updated on July 05, 2022

Comments

  • Vikram
    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
    Vikram about 7 years
    But still am getting the same error i tried what you suggested but i could not resolve the issue.
  • twm
    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
    Vikram about 7 years
    contentType="Content-Type: application/json" How to include it with header like -H ${contentType} does not work. Any workaround
  • Barmar
    Barmar about 7 years
    Did you read my answer? What does it say about quoting variables?
  • Barmar
    Barmar about 7 years
    @Vikram 99% of the time you want to quote variables when you use them.
  • Barmar
    Barmar about 7 years
    It should work. Update the question to show exactly what you're doing.
  • Barmar
    Barmar about 7 years
    Or post a new question, since you've already marked this question as solved.
  • Vikram
    Vikram about 7 years
    contentType="Content-Type: application/json" curl --silent --insecure -X POST -d "${post}" -H "${contentType}" $restUrl but here it returns 415
  • Vikram
    Vikram about 7 years
  • Barmar
    Barmar about 7 years
    You can't put the variable assignment on the same line that you use the variable.
  • Barmar
    Barmar about 7 years
    All variables are expanded before executing the command, so it doesn't execute the assignment until after the variable is expanded.
  • Vikram
    Vikram almost 7 years
    Can 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)