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:
- Quotes inside expanded variables do not work as if you typed them in directly.
- 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
Bash -C Variable Does Not Get Assigned
Setting Environment Variable to a Large Value -> "Argument List Too Long"
Using $Origin to Specify the Interpreter in Elf Binaries Isn't Working
Script Produces Different Result When Executed by Bash Than by Cron
File Glob Patterns in Linux Terminal
Idle Time of a Process in Linux
Bash Script - "Tar Czf ..." Command Ignore Parameters
How to Read from Text File Line-By-Line and Split the Line by a Character
Opening New Gnome-Terminal (V3.28+) with Multiple Tabs and Different Commands
Asp Net Core Linux Err_Connection_Refused
Cmsg_Nxthdr() Returns Null Even Though There Are More Cmsghdr Objects
Print the Output of Strace Command in a Text File
How to Set Firefox Binary Path of Firefox in Selenium in Linux