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
Related
I am quite new to Elastic and bash script . Could anyone please help me with the below -
How can i skip creating the elastic index which already exists using bash
First I am creating a mapping in a bash script -
#!/bin/bash
curl --user uid:password -X PUT **"https:localhost/newindex"** -H 'Content-Type: application/json' -d'{ "mappings": { "properties": {.....}}}}'
#Then i am adding the data to it
curl --user uid:password -X POST **"https:localhost/newindex"** -H 'Content-Type: application/json' -d'{"date":"data..."...... }'
If i run it for the first time it works , but when i run for the second time onwards it gives me an error "resource_already_exists_exception" , how can i handle this using bash script
You can use --fail flag:
-f, --fail (HTTP) Fail silently (no output at all) on server errors. This is mostly done to better enable scripts etc to better deal with failed attempts. In normal cases when an HTTP server fails to deliver a document, it returns an HTML document stating so (which often also describes why and more). This flag will pre‐ vent curl from outputting that and return error 22.
The exit code will be zero if the command has succeeded, so you can check that:
curl --fail --user uid:password -X POST "https:localhost/newindex" -H 'Content-
Type: application/json' -d'{"date":"data..."...... }'
if [ 0 -eq $? ]; then … fi;
I wrote a curl script to pull data from my api url into my unix terminal. But when I run the script below, I get a blank space as my output, rather than data from the api url. I don't get any standard error message, just a blank space followed by $ to enter a new command.
#!/bin/bash
INSTANCE_NAME="https://servicenow.com/ServiceNowData/tickets?ticks=4320000"
DATA_OUTPUT=$(curl -s -k -X GET -H "accept: application/json" $INSTANCE_NAME)
echo $DATA_OUTPUT
Page 404. And -H "Accept-Encoding: application/json"
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.
I have been trying to execute curl command from a script file.
The command is as follows (the IP address is stored in variable ip):
curl -s -X POST ${ip1} -H \"content-type: application/json\" -d \''{"args":["org1","scatest'$j$i'","27-06-2018"]}'\'
It's throwing an error saying "cannot read property", if I execute from the script file.
Whereas if I execute from the command line then there is no problem.
Can anybody help me out why curl command is not getting executed from the script file?
Since the discussions under the comment has lead to a solution that works well. I'm gonna put it into answer for the record and in case anyone else runs into the same pitfall.
The problem was caused by excessive quoting which would result in extra quotes being made part of the request, correct form of the command was:
curl -s -X POST ${ip1} -H "content-type: application/json" \
-d '{"args":["org1","scatest'$j$i'","27-06-2018"]}'
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