Run curl from .sh script with defined Content-Type - bash

When I'm trying to run test.sh script I've always receive error from curl:
curl: (6) Couldn't resolve host 'application'
test.sh:
#!/bin/sh
CT="Content-Type:\ application/json"
TEST="curl http://127.0.0.1 -H $CT"
echo $TEST
RESPONSE=`$TEST`
echo $RESPONSE
But if I just run following command from console everything fine:
curl http://127.0.0.1 -H Content-Type:\ application/json
Could you please let me know what is wrong in script, as I understand something is wrong with 'space' escape, but have no idea how to fix it.
Also I've tried following combination, but result the same:
CT="Content-Type: application/json"
TEST="curl http://127.0.0.1 -H \"$CT\""
UPD:
bash / dash is only available on server. (/bin/sh --> bash)
GNU bash, version 4.2.10(1)-release (x86_64-pc-linux-gnu)

Run the following: (delete the space after Content-Type)
#!/bin/bash
CT="Content-Type:application/json"
TEST="curl http://127.0.0.1 -H $CT"
echo $TEST
RESPONSE=`$TEST`
echo $RESPONSE

You can try with: bash -c your_bash_file.sh It worked for me with the same problem

Related

curl 400 bad request for bash script

I am trying to run a curl command within a bash, when running the curl outside separately its working fine, but same running with Bash throws bad request 400 error. Would appreciate if someone can point where I am going wrong
#!/bin/bash
TIMESTAMP=$(date +%Y-%m-%d_%H-%M-%S)
curl http://localhost/cron/abc -H 'content-type: application/json'
echo 'Cron Executed at:' "$TIMESTAMP" | tee /var/log/cron.txt

How can I pass all arguments to another command, some of which are quoted and contain spaces?

I want to pass multiple arguments through to curl. Some of these arguments are quoted and contain spaces.
I have tried like this:
ARGS="http://example.org -H 'My-Header: Foo'"
curl -vvv $ARGS
But my header is not set and I get an error at the end curl: (6) Could not resolve host: Foo'.
I have also tried quoting ARGS like this:
ARGS="http://example.org -H 'My-Header: Foo'"
curl -vvv "$ARGS"
But I get curl: (3) URL using bad/illegal format or missing URL.
If I just run curl with the arguments directly, then it works fine:
curl -vvv http://example.org -H 'My-Header: Foo'
How can I pass these arguments through to curl correctly?
There is a command called eval which evaluates all the arguments into one string and then runs it as a one big command.
Try eval curl $ARGS
I recommend you to checkout eval's man page ;)

curl 400 bad request (in bash script)

I trying to do execute the following script in bash
#!/bin/bash
source chaves.sh
HEAD='"X-Cachet-Token:'$CACHET_KEY'"'
SEARCH="'{"'"status"'":1,"'"id"'":"'"7"'","'"enabled"'":true}'"
echo $SEARCH
if curl -s --head --request GET http://google.com.br | grep "200 OK" > /dev/null; then
echo 'rodou'
curl -X PUT -H '"Content-Type:application/json;"' -H '"'X-Cachet-Token:$CACHET_KEY'"' -d $SEARCH $CACHET_URL/7
else
echo 'não deu'
curl -X PUT -H '"Content-Type: application/json;"' -H $x -d '{"status":1,"id":"7","enabled":true}' $CACHET_URL/7
fi
But keep receiving a 400 bad request from the server.
When i try to run the same line (echo in the script, Ctrl+c and Ctrl+v) directly in terminal, the command run without problems.
The source file have the directions to path and a variable token i need to use, but as far as i have tested is reading ok.
edit 1 - hidding some sensitive content
edit 2 - posting the exit line (grabed trought Ctrl+c, Ctrl+v)
The command i neet to input in server is:
curl -X PUT -H "Content-Type:application/json;" -H
"X-Cachet-Token:4A7ixgkU4hcCWFReQ15G" -d
'{"status":1,"id":"7","enabled":true}'
http://XXX.XXX.XXX.XXX/api/v1/components/7
And the exit i grabed trought echo comand, give me the exact exit i want, but don't run inside script, only outside.
I'm a bit new to the curl, any help can be apreciate.
Sorry for the bad english and tks in advance.

How to use CURL over SSH and get the file as well as the return value?

I want to load a file from a clients webserver. This webserver is running local only. To get there I have to use ssh. I need the content as well as the return value (e.g. SSH connection broke, webserver down).
What do I have to change? My first try:
#!/bin/bash
RETURN=0
CONTENT=""
sshpass -p xxxxxx ssh root#172.17.1.33 "curl -X POST http://127.0.0.1:10000/status -H 'Content-Type: application/json' > $CONTENT | bash; RETURN=$?"
If you want to get the exit code of curl and the return value of curl:
#!/bin/bash
CONTENT=$(sshpass -p xxxxxx ssh root#172.17.1.33 "curl -X POST http://127.0.0.1:10000/status -H 'Content-Type: application/json'")
RETURN=$?
echo "$RETURN, $CONTENT"
In your script you set the variables on the server you ssh'ed into.

How to retrieve error code from cURL on shell

I know a similar question was posted, but I can't get it to work on my machine.
I tried the 1st answer from the mentioned question, i.e. response=$(curl --write-out %{http_code} --silent --output /dev/null servername) and when I echo $response I got 000 [Not sure if that is the desired output].
However, when trying to do so with my cURL command, I get no output.
This is my command:
curl -k --silent --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt
and I use it with
x=$(curl -k --silent --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt)
but when I try to echo $x all I get is a newline...
I know the cURL is failing, because when I run the same command, without --silent, I get curl: (7) Couldn't connect to server
This Q is tagged with both sh, bash because I've tried it on both with same results
I found this option which kind of helps (but I still don't know how to assign it to a variable, which should be easier than this...):
--stderr <file>
Redirect all writes to stderr to the specified file instead. If the file name is a plain '-', it is instead written to stdout.
If this option is used several times, the last one will be used.
When I use it like this:
curl -k --silent -S --stderr my_err_file --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt
I can see the errors (i.e. curl: (7) Couldn't connect to server) inside that file.
I used --silent to suppress all output, and -S to un-suppress the errors, and the --stderr <file> to redirect them

Resources