Curl Command Doesn't Work in Bash Script

curl command not working from within shell script but works fine out site

First of all, don't store commands in variables.
Apart from that, there are two problems:

  1. Quotes inside expanded variables do not work as if you typed them in directly.
  2. The subshell $() around $($CMD) seems suspicous. Not only do you execute the command stored in $CMD, but you also execute the output of that command!

1. Quotes inside variables

When you enter the command echo "a b" then bash will process that command such that echo is executed with the argument a b. The output will be a b.

When you store that command inside a variable and expand that variable, bash will process the variable's content differently. The command echo will be executed with the arguments "a and b". The output will be "a b".

This is the reason for the error message

curl: (26) couldn't open file "/var/log/tyk/2018-October-18-10.csv""

curl is trying to open a path with an actual quote inside. Such a path does not exist on your system.

To get around this issue, you could write eval "$cmd", then the command would be executed as if you typed it in directly. However, you really shouldn't. I'd rewrite the script instead to not store commands in variables.

2. Subshell around $cmd:

cmd='echo something'
$cmd

This would print something.
However, your script doesn't stop there, because you enclosed $cmd in $(). Therefore the output something gets executed too.

cmd='echo something'
$($cmd)

results in

bash: something: command not found

curl works, but won't execute in BASH script

Bash and cURL can be quite particular how quotes are used within a script. If the escaping gets thrown off then everything else can easily fail. Running the script through shellcheck.net is often very helpful in identifying such issues. Below is a revised version of the script after fixing based upon the suggestions:

#!/bin/bash

baseUrl="https://somebaseurl/api/v1/auth/login"
contentTypeJson="Content-Type:application/json"
credentials="{\"email\": \"foo@bar.com\", \"password\": \"foo\"}"
login="$(curl -X POST "$baseUrl" -H "$contentTypeJson" -d "$credentials")"
echo "${login}"

response="${login}"
echo "${response}"

curl command within bash file doesn't work

You shouldn't have \' around your variables. That will put a literal single quote into that argument.

Use double quotes to make a variable expand as a single argument.

curl -X POST "$URL" -H "$HEADERS" -d "$data"

Curl command doesn't work in bash script

Don't store a command in a variable; if you absolutely must have something usable with logging, put the arguments in an array.

test='{"evaluation": "none"}'
args=( -XPUT localhost9200/test/evaluation/"$i" -d "$test" )
echo "curl ${args[*]}"
curl "${args[@]}"

cURL command doesn't work from terminal prompt

Replace tabs with spaces, and drop the last backslash, then it should work just fine.

curl -X POST https://api.mailerlite.com/api/v2/campaigns \
-d '{
"subject": "Regular campaign subject",
"segments": 1291259,
"type": "regular"
}' \
-H "Content-Type: application/json" \
-H "X-MailerLite-ApiKey: <api-key>"

It gives the following output to me:

$ curl -X POST https://api.mailerlite.com/api/v2/campaigns \
> -d '{
> "subject": "Regular campaign subject",
> "segments": 1291259,
> "type": "regular"
> }' \
> -H "Content-Type: application/json" \
> -H "X-MailerLite-ApiKey: <api-key>"
{"campaign_type":"regular","date":"2020-10-06 19:21:47","account_id":<id>,"id":<id>,"mail_id":<id>,"options":{"current_step":"step3","send_type":"regular","campaign_type":"regular","date":"2020-10-06 19:21:47"}}

curl command not executing via shell script in bash

A command embedded within a parenthesis runs as a sub-shell so your environment variables will be missing.

Try eval:

curlCmd="curl 'https://www.facebook.com/vivekkumar27june88' > ~/Desktop/fb.html"
eval $curlCmd

Can not pass parameter into Curl Command as Bash Script

Converting my comment to answer so that solution is easy to find for future visitors.

Following should work for you with proper quoting that allows $1 to expand:

curl -ki \
--cookie cookie-jar.txt \
--header 'Content-Type: text/xml' \
--request POST https://ugtestpo:8446/v1/secondarylogin \
--data '<?xml version="1.0" encoding="UTF-8"?>
<p:secondaryloginrequest xmlns:p="http://www.ericsson.com/em/am/co/authentication/v1_0/">
<credential>
<secret>'"$1"'</secret>
<type>otp</type>
</credential>
<channelinformation>COMVIVAAGENTAPP</channelinformation>
</p:secondaryloginrequest>'


Related Topics



Leave a reply



Submit