Curl command doesn't work in bash script - linux

I am trying to upload a JSON file into my noSQL database using a bash script, but it doesn't work and I don't understand why.
This is the script :
test='{"evaluation": "none"}'
test="'$test'"
command="curl -XPUT localhost:9200/test/evaluation/$i -d $test"
echo "$command"
$command
This is the error :
curl -XPUT localhost:9200/test/evaluation/0 -d '{"evaluation": "none"}'
{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}curl: (3) [globbing] unmatched close brace/bracket in column 7
When I do the command given in my command line it works fine though.
What is the error here ? Thank you

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[#]}"

Related

How to extract text from web and add into another file in shell script

I've one shell script where i mentioned as
#!/bin/sh
.........
curl -X POST -d text={varX} $WEB_LINK
.............
So I want to extract text of WEB_LINK and add into another file like xyz.txt. Could you please help me to do it.
Try this:
#!/bin/sh
text=$(curl -s $WEB_LINK)
echo $text > xyz.txt
Try following:
#!/bin/sh
cmd="curl -X POST -d text={varX} $WEB_LINK > xyz.txt"
bash -c "$cmd"

Using printf %q to make a quoted string usable as shell script input

Within a bash script, I am trying to append a command string that is single and double quoted to a file (.profile).
I would like to use echo and then >> the command to .profile. Of course, I am open to any solution that works.
The command I would like to use is echo "curl -X POST -H "Content-Type: application/json" -d '{"value1":"PHONENUMBER","value2":"MESSAGE"}' https://maker.ifttt.com/trigger/TRIGGER/with/key/KEY &> /dev/null" >> .profile but clearly this doesn't work within my bash script.
I am not clear on how printf %q works and don't understand how to apply it to my problem.
I have tried this
`CMDSTRING='curl -X POST -H "Content-Type: application/json" -d '`
`CMDSTRING=${CMDSTRING}"'"`
`CMDSTRING=${CMDSTRING}'{"value1":"+PHONENUMBER","value2":"MESSAGE"}'`
`CMDSTRING=${CMDSTRING}"'"`
`CMDSTRING=${CMDSTRING}' https://maker.ifttt.com/trigger/TRIGGER/with/key/KEY &> /dev/null'`
`echo $CMDSTRING`
Using printf '%q' to generate .profile content looks something like the following:
{
printf '%q ' \
curl -X POST -H "Content-Type: application/json" \
-d '{"value1":"PHONENUMBER","value2":"MESSAGE"}' \
https://maker.ifttt.com/trigger/TRIGGER/with/key/KEY
printf '%s\n' "&>/dev/null"
} >> .profile
Note that you cannot use the %q format string if you want &>/dev/null to be parsed as syntax, since by its very nature it formats everything it's passed to be parsed as data.
Thus, we use printf '%q ' "command name" "first argument" ... for the actual command itself, and format the redirection out-of-band.
That said, note that there's value to the above only if you're substituting variables from an untrusted source (rather than hardcoding them as in the example), and are worried about invalid values being abused for command injection. If you're truly just appending a constant string to the end of a file, a quoted heredoc will let you build more natural-looking shell quoting manually (indeed, as you've already done!), and pass it through verbatim:
cat >>.profile <<'EOF'
curl -X POST -H "Content-Type: application/json" \
-d '{"value1":"PHONENUMBER","value2":"MESSAGE"}' \
https://maker.ifttt.com/trigger/TRIGGER/with/key/KEY &> /dev/null
EOF
Here, everything between the <<'EOF' and the EOF are passed through exactly-as-given, including quotes and parameter expansions the shell might otherwise try to interpret.

expanding shell variable in curl POST?

I use the following line to create database:
curl -X POST 'http://10.1.1.1:8086/db?u=root&p=root' -d '{name: test1}
if i try to do it from shell script:
ip=10.1.1.1
curl -X POST 'http://$ip:8086/db?u=root&p=root' -d '{name: test1}'
i have a problem with shell variable substitution within single quotas, if i try to use them within double quotas:
curl -X POST "http://$ip:8086/db?u=root&p=root" -d '{name: test1}'
variable is expanded to the right value, printing in terminal
curl -X POST "http://10.1.21.1:8086/db?u=root&p=root" -d '{name: test1}': **No such file or directory**
What would be the right solution to this problem?
Try this:
ip=10.1.1.1
curl -X POST 'http://'"$ip"':8086/db?u=root&p=root' -d '{name: test1}'

Linux cURL XML file POST - how to check for error/success?

I am using cURL to post a XML file on Linux as follows:
curl -X POST --header "Content-Type: text/xml" -d #test.xml "https://www.example.com"
How can I check the status of the cURL command to see if the file was posted or not?
Thanks for any help.
I'm not sure if I got you but you can basically check the return value of the curl command. The return value of the last command is stored in the variable $?.
Example:
curl -X POST --header "Content-Type: text/xml" -d #test.xml "https://www.example.com"
ret=$? # store return value for later usage in the error message
if [ $ret != 0 ] ; then
echo "POST failed with exit code $ret"
fi
This list of possible error codes can be found at the bottom of the man page. They are very helpful for debugging.
You can get the response status of your operation using -w %{http_code}
curl -s -o out.txt -w %{http_code} http://www.example.com/
In this example -s means silent mode, and -o out.txt means to save the response(usually html) into a file.
For this above command, you'l have output 200 when its success.

BASH: How to send params & data to a process on STDIN

I'm scripting a call to curl, you can enter the password & parameters via STDIN (keep password off the cmd line).
I also need to send POST data on STDIN (large amount of data that won't fit on the cmd line).
So, from a command line I can successfully do this by:
> curl -K --data-binary #- -other_non-pw_params
> -u "username:password"
> <User types ctrl-d>
> lots_of_post_data
> lots_of_post_data
> <User types ctrl-d>
> <User types ctrl-d>
Now... I'm trying to do that in a BASH script...
Wishful-thinking Psudo-code:
{ echo '-u "username:password"'
echo <ctrl-d> | cat dev/null | ^D
echo lots_of_post_data
echo lots_of_post_data
} | curl -K --data-binary #- -other_non-pw_params
Aha! There's a curl specific solution to this.
You pass all of the parameters on STDIN, and leave --data-binary #- (or it's equivalent) to the end, then everything after it is accepted as data input. Example script:
#!/bin/bash
{ echo '--basic'
echo '--compress'
echo '--url "https://your_website"'
echo '-u "username:password"'
echo '--data-binary #-'
echo 'lots_of_post_data'
echo 'lots_of_post_data'
} | curl --config -
Use a "here document":
curl --config - <<EOF
--basic
...
EOF
There is no way to simulate a EOF as in Ctrl-D in the terminal save to stop sending data to the stream altogether. You will need to find a different way of doing this, perhaps by writing a script in a more capable language.

Resources