I have this section of curl command inside my shell script which caused a error 500 repsonse from the server. However, this code works when I manually run the command.
curl -qgsSkH "Content-Type: multipart/form-data" --no-progress-bar --header "Token: $authKey" -F "filename=@$compressedFileName" -F "options={\"application\":\"2\",\"timeout\":\"5000\"}" https://www.domain.com > results.txt
Anyone have any idea why?
Edit: I did some manual debugging and found out that the issue is caused by the variable $authKey. This variable is fetched by the following code in my script.
# Get authentication key from FireEye AX response
while read line
do
if [[ $line =~ ^SOME-PATTERN:[[:space:]](.*) ]]
then
authKey="${BASH_REMATCH[1]}"
fi
done <auth.txt
If I redefine this variable by using something like the following then the script will work.
authKey="TheAuthenticationKey"
But why is this happening? The earlier part where I pass the string from the file when I read it line by line is already passing the correct value (I double checked by echoing the output to a txt file).
Edit I managed to drill down somemore. When I try to echo the value of the token to a file and add " to the start and the end of the string I got this result.
"X-FeApi-Token: IDR+jxU1kB5ZpXsRNAgqfjTxkjZ0L2atH+l0H/NeatWDAJs=
"
I am not sure how to remove that new line thingy. Anyone can advise?
=~
should handle that. try adding printf debugging statements or run your script withbash -x
to see what's going on.(.*)
will capture everything to the end of the line, including the EOL marker (LF or CR-LF). remove them with... | tr -d '\r\n'
.printf "%s" "$authKey" | sed -n l
.