0

I am working on a self project which is an API server. I am trying to test my api routes using curl command with bash scripts.

The curl commands for post requests are pretty long so I want to use a bash script where I store request methods, routes, payloads to simplify this command

curl -X POST -H "Content-Type: application/json" -d '{ "title": "Work on project/manager shell", "priority": 1, "description": "Complete and test all the routes in the shell with curl" }' http://localhost:4444/task/add

to

curl -X $method -H "Content-Type: application/json" -d $payload http://localhost:4444/$route

This should work fine, but I don't know why $payload is not unwrapping correctly inside the command, but it is exactly equal to '{ "title": "Work on project/manager shell", "priority": 1, "description": "Complete and test all the routes in the shell with curl" }'

My bash script and the output after running it is as follows:

test.sh

method="POST"
route="task/add"
addtask=$(cat <<EOF
{
  "title": "Work on project/manager shell",
  "priority": 1,
  "description": "Complete and test all the routes in the shell with curl"
}
EOF
)

payload=$(quote "${addtask}")
echo $payload

# This does not work
curl -X $method -H "Content-Type: application/json" -d $payload http://localhost:4444/$route

# But this does?
curl -X $method -H "Content-Type: application/json" -d '{ "title": "Work on project/manager shell", "priority": 1, "description": "Complete and test all the routes in the shell, use proper try-catch to ensure error free api service" }' http://localhost:4444/$route

output

ck@ck-IdeaPad-Gaming-3-15ARH05:~/projects/manager$ source test.sh
'{ "title": "Work on project/manager shell", "priority": 1, "description": "Complete and test all the routes in the shell with curl" }'
curl: (6) Could not resolve host: "title"
curl: (6) Could not resolve host: "Work
curl: (6) Could not resolve host: on
curl: (6) Could not resolve host: project
curl: (6) Could not resolve host: shell",
curl: (6) Could not resolve host: "priority"
curl: (6) Could not resolve host: 1,
curl: (6) Could not resolve host: "description"
curl: (6) Could not resolve host: "Complete
curl: (6) Could not resolve host: and
curl: (6) Could not resolve host: test
curl: (6) Could not resolve host: all
curl: (6) Could not resolve host: the
curl: (6) Could not resolve host: routes
curl: (6) Could not resolve host: in
curl: (6) Could not resolve host: the
curl: (6) Could not resolve host: shell
curl: (6) Could not resolve host: with
curl: (6) Could not resolve host: curl"
curl: (3) unmatched close brace/bracket in URL position 1:
}'
 ^
{"owner":"641bf07b39c5c4d727fd3e87","title":"Work on project/manager shell","priority":1,"description":"Complete and test all the routes in the shell, use proper try-catch to ensure error free api service","completed":false,"_id":"642d64a457f8998c0cd8d8f5","createdAt":"2023-04-05T12:08:04.665Z"}ck@ck-IdeaPad-Gaming-3-15ARH05:~/projects/manager$ 

How do I make the second command work? I am just confused as I feel both commands should be exactly same.

  • 1
    Learn how to quote properly in shell, it's very important : > "Double quote" every literal that contains spaces/metacharacters and _every_ expansion: `"$var"`, `"$(command "$var")"`, `"${array[@]}"`, `"a & b"`. Use `'single quotes'` for code or literal `$'s: 'Costs $5 US'`, `ssh host 'echo "$HOSTNAME"'`. See [when-is-double-quoting-necessary](https://unix.stackexchange.com/questions/68694/when-is-double-quoting-necessary) – Gilles Quénot Apr 05 '23 at 12:16
  • This helped @GillesQuénot, it worked without `payload=$(quote "${addtask}")`, and just by using `"$addtask"`. I was trying to make the command exactly like the expanded one, that's why I used `quote`. But why does `$method` and `$route` work without double quotes whereas `$addtask` needs to be double quoted? – Chirag Kadam Apr 05 '23 at 12:33
  • See also https://mywiki.wooledge.org/WordSplitting oh okay there was a pile of links already. Anyway, the Word Splitting page has that helpful script you can use to test it and see what actually happens in the various cases – ilkkachu Apr 05 '23 at 12:38
  • @ChiragKadam please read the answers to the question that has been marked as a duplicate. Also see [Why does my shell script choke on whitespace or other special characters?](https://unix.stackexchange.com/q/131766) and [Security implications of forgetting to quote a variable in bash/POSIX shells](https://unix.stackexchange.com/q/171346). In the specific case of your variables, they work because the value is just an alphanumeric string with no spaces or special characters. – terdon Apr 05 '23 at 12:38
  • Thanks @terdon, this clears my doubt, I'll go through the resources shared – Chirag Kadam Apr 05 '23 at 12:40

0 Answers0