Issue
I have below script to run a curl command from Linux host, which takes user inputs and runs but its not turning successful while same command manually succeeds.
#!/bin/bash
# Constants
api_key="Thdshd760Jd"
api_url="https://execute-api.amazonaws.com/prod/v1/archive;done"
# User input
read -p "Enter projectName: " projectName
read -p "Enter siteName: " siteName
read -p "Enter emailAddress: " emailAddress
read -p "Enter the path to the file containing disk names: " disk_file
# Read disk names from the file and create a comma-separated string
disks=$(awk '{printf "\"%s\", ", $1}' "$disk_file" | sed 's/, $//')
# Create JSON payload in Bash
json_payload="'{\"projectName\": \"$projectName\", \"siteName\": \"$siteName\", \"disks\": [$disks], \"emailAddress\": \"$emailAddress\"}'"
# Make the API request using curl
curl -vv -X POST -H "x-api-key: $api_key" -H "Content-Type: application/json" -d "$json_payload" $api_url
echo 'curl -vv -X POST -H "x-api-key: '$api_key'" -H "Content-Type: application/json" -d '$json_payload' '$api_url''
Below how i am executing it ..
$ ./curl_api_call.sh
Enter projectName: my_disk001
Enter siteName: JLDC
Enter emailAddress: [email protected]
Enter the path to the file containing disk names: demo_file
Note: Unnecessary use of -X or --request, POST is already inferred.
{"message": "Invalid request body"}
curl -vvvvvv -X POST -H "x-api-key: Thdshd760Jd" -H "Content-Type: application/json" -d '{"projectName": "my_disk001", "siteName": "JLDC", "disks": ["my_disk001"], "emailAddress": "[email protected]"}' https://execute-api.amazonaws.com/prod/v1/archive
even doing like giving same error:
for i in `cat` ; do curl -vvvvvv -X POST -H "x-api-key: Thdshd760Jd" -H "Content-Type: application/json" -d '{"projectName": "$i", "siteName": "JLDC", "disks": ["$i"], "emailAddress": "[email protected]"}' https://execute-api.amazonaws.com/prod/v1/archive;done
please guide me on how this can be corrected.
Solution
The single quotes in the JSON assignment are wrong.
# Create JSON payload in Bash
json_payload="'{\"projectName\": \"$projectName\", \"siteName\": \"$siteName\", \"disks\": [$disks], \"emailAddress\": \"$emailAddress\"}'"
# ^ ^
Similarly, in the for
loop attempt, the single quotes prevent the variables from being interpolated.
for i in `cat` ; do
curl -vvvvvv -X POST -H "x-api-key: Thdshd760Jd" \
-H "Content-Type: application/json" \
-d '{"projectName": "$i", "siteName": "JLDC", "disks": ["$i"], "emailAddress": "[email protected]"}' https://execute-api.amazonaws.com/prod/v1/archive;done
# ^ ^
Try this:
json_payload="{\"projectName\": \"$projectName\", \"siteName\": \"$siteName\", \"disks\": [$disks], \"emailAddress\": \"$emailAddress\"}"
and similarly
while read -r i; do
curl -vvvvvv -X POST -H "x-api-key: Thdshd760Jd" \
-H "Content-Type: application/json" \
-d "{\"projectName\": \"$i\", \"siteName\": \"JLDC\", \"disks\": [\"$i\"], \"emailAddress\": \"[email protected]\"}" \
https://execute-api.amazonaws.com/prod/v1/archive;done
done
(Also don't read lines with for
.)
Answered By - tripleee Answer Checked By - Mildred Charles (WPSolving Admin)