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.

0 Answers0