I am executing the below curl command in shell script. But I am getting command not found error. So wanted to check if I am making any mistake while executing it.
return = $(curl -s --location --request GET --url 'https://testurl.mywebsite.com/api/accts?Id=trst_id&var=test_var' --header 'type: app/test')
echo "data is: ${return}"
I am getting the error:
return: command not found
Kindly let me know if there is a problem with the way I am trying to execute the curl command.
Thank you.
Like dan pointed out in comments, the extra whotespaces between the variable and command caused the issue. After removing extra whitespaces it worked.
return=$(curl -s --location --request GET --url 'https://testurl.mywebsite.com/api/accts?Id=trst_id&var=test_var' --header 'type: app/test')
echo "data is: ${return}"
Related
I'm trying to get values from nexus and assign it to a variable using curl command
The curl command i use
curl -u user_name:password --request GET "http://host_name:portnumber/URL
This works as expected in postman
when i try assigning this curl command to a variable and print it with this command
var=$(curl -u user_name:password --request GET "http://host_name:portnumber/URL)
echo $var
I couldn't see the value i get when i use the curl command in postman .
Please help to on this issue . thanks in advance .
I have a bash script that loops through all files in a directory and makes a curl request to some URL
for FILE in ./files/*;
do
echo "Making a request..."
echo $FILE
curl --location --request POST "${URL}" \
--form 'file=#"${FILE}"' \
sleep 100
done
echo "done!"
The curl request was copied from postman so I'm confident that it works.
When I run the script, I get the following
Making a request...
./files/split1.csv
curl: (26) Failed to open/read local data from file/application
The issue I'm getting is how to handle string interpolation here.
You are very close, just remove the single quotes to get the string interpolation to work.
curl --location --request POST "${URL}" \
--form file=#"${FILE}" \
This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 2 years ago.
I am having issues with sending the proper json data in a curl -X POST command. I have successfully run the POST command locally on my mac by copy and pasting the hardcoded values in but I am now trying to create a .sh script to automate this process. Upon running the code below I get this error:
{"message":"Unexpected end of JSON input"}
Here is the output json from JSON_STRING with names made generic and nothing else changed:
{ "url": "api_url", "tileset": "username.filename" }
Once I can figure out how to properly format the json in the POST command I know it will work, but I can't seem to get the syntax right. Hoping a set of fresh/experience bash eyes will be able to catch my mistake:). Also, all variables that I have are correct and already been confirmed by running variable values in mac terminal. Thanks in advance for the help!
curl_url="http://${bucket}.s3.amazonaws.com/${key}"
echo $curl_url
tileset_id="username.filename"
JSON_STRING=$(jq -n \
--arg bn "$curl_url" \
--arg on "$tileset_id" \
'{url: $bn, tileset: $on}')
echo $JSON_STRING
curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d $JSON_STRING 'apiurl'
Absolutely required to quote the shell variable:
curl ... -d "$JSON_STRING" http://example.com/end/point
Otherwise, the shell will do word splitting, and the argument to -d becomes just {"url":
as a side note, bash arrays can help with the readability:
curl_url="http://${bucket}.s3.amazonaws.com/${key}"
tileset_id="username.filename"
JSON_STRING=$(
jq -n \
--arg bn "$curl_url" \
--arg on "$tileset_id" \
'{url: $bn, tileset: $on}'
)
curl_opts=(
-X POST
-H "Content-Type: application/json"
-H "Cache-Control: no-cache"
-d "$JSON_STRING"
)
curl "${curl_opts[#]}" 'apiurl'
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"]}'
I am passing username and password to the curl command to receive the output from the api call. Not sure whats wrong, but the curl command works from the command line, but when I use the same curl command within a bash script, it doesn't take the credentials properly. API call fails with an authorization error. Can someone throw some pointers here?
curl --silent -u 'blah-blah:youareawesome$1234' https://example.com/api/check
Here is the script
USERNAME=$1
PASSWORD=$2
curl --silent -u "${USERNAME}:${PASSWORD}" https://example.com/api/check
{"timestamp":1509422967185,"status":401,"error":"Unauthorized","message":"Bad credentials","path":"/api/check"}
Try this
UNAME=$1
PASSWORD=$2
curl --silent -u "${UNAME}:${PASSWORD}" https://example.com/api/check
Blockquote
curl --silent -u 'blah-blah:youareawesome$1234' https://example.com/api/check
This might be a red herring on the quotes but your script won't accept strings after the $ sign.
Might need to wrap your username & password in quotes as you give your script your inputs.
Thank you for all the answers !! For whatever reason, after i modified the script to have the username and password as separate variable and passing to the curl command worked
CREDS="${USERNAME}:${PASSWORD}"
CURL_CMD="curl --silent -u ${CREDS}"
${CURL_CMD} https://example.com/api/check
You can use like this,
example:
curl --location --request POST 'http://119.235.1.63/test' \
--header 'Content-Type: application/json' \
--data-raw '{ "CompanyId":"mafei", "Pword":"mafei123", "SmsMessage":"I am '${USER}'", "PhoneNumber": [ "712955386"] }'
in your code please use single quotes like below
curl --silent -u "'${USERNAME}:${PASSWORD}'" https://example.com/api/check