Home > Mobile >  Curl command not taking in the right variable
Curl command not taking in the right variable

Time:11-02

There seems to be a problem with the curl command i dont understand and cant seem to find the right answer. I try to write a specific part of a curl command output (an url behind "location") to a variable and then use this variable in another curl command. My variable is named LINK.

LINK=$(curl -X GET 'https://address.net/wiki/download/attachments/2761195537/htdocs.zip?api=v2' --user myMail.com:6N6usevDb7qDthqrsxJeE655 -I | grep -m1 'location' | cut -f2- -d" ") 
  • address.net is the placeholder url of the website
  • myMail.com is the placeholder for my E-mail
  • 6N6usevDb7qDthqrsxJeE655 is the auth token i changed randomly for display in this question.

If i echo the variable like

echo $LINK

i get the correct output:

https://address.net/file/5ad2b26e-9aa3-462d-980c-323adde372d5/binary?token=eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJkYzNiYTU5Ny01NDNkLTQ4Y2QtOTM1ZC00ZTYwNjkyMGJkYzAiLCJhY2Nlc3MiOnsidXJuOmZpbGVzdG9yZTpmaWxlOjVhZDJiMjZlLTlhYTMtNDYyZC05ODBjLTMyM2FkZGUzNzJkNSI6WyJyZWFkIl19LCJleHAiOjE2NjY5NTg3NDAsIm5iZiI6MTY2Njg3NTgyMH0.cYkWvyd358Z2IRUmNXOWWYAI_h-X0Xvm8vaNUT71Xho&client=dc3ba597-543d-48cd-935d-4e606920bdc0&name=htdocs.zip

But if i try to use this variable in another curl command (to download the so named htdocs.zip) the url is cut off. The command:

echo curl -v "'$LINK'" --user myMail.com:6N6usevDb7qDthqrsxJeE655 --output htdocs.zip

Gets me the following output:

curl -v 'https://address.net/file/5ad2b26e-9aa3-462d-980c-323adde372d5/binary?token=eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJkYzNiYTU5Ny01NDNkLTQ4Y2QtOTM1ZC00ZTYwNjkyMGJkYzAiLCJhY2Nlc3MiOnsidXJuOmZpbGVzdG9yZTpmaWxlOjVhZDJiMjZlLTlhYTMtNDYyZC05ODBjLTMyM2FkZGUzNzJkNSI6WyJyZWFkIl19LCJleHAiOjE2NjY5NjE3NzYsIm5iZiI6MTY2Njg3ODg1Nn0.BiUNe_QK0PVKduQZssr_sncY9KxfLKb3UkL0TxiIB_8&client=' --user myMail.com:6N6usevDb7qDthqrsxJeE655

Notice that at the end of the second url output some of the url is missing which was still there using echo $LINK previously. The part dc3ba597-543d-48cd-935d-4e606920bdc0&name=htdocs.zip of the url is missing. I dont understand why that is the case if i use the same variable twice.

I also tried writing the variable to a file.

curl -v https://address.nett/wiki/download/attachments/2761195537/htdocs.zip?api=v2 --user` `myMail.com:6N6usevDb7qDthqrsxJeE655 &>> MyText.txt`
grep -m1 location /home/user/Desktop/MyText.txt > /home/user/Desktop/MyTextA.txt
sed -i "s/< location: //g" MyTextA.txt
LINK=$(<MyTextA.txt)

Still using this Variable in the same curl command from above

curl -v "'$LINK'" --user myMail.com:6N6usevDb7qDthqrsxJeE655 --output htdocs.zip

is missing part of the url from the LINK variable.

CodePudding user response:

It would be best if you use the -w/--write-out option to get the response header:

curl -w '%header{location}\n' google.com -I

Other things, I guess the -I option will override the -X GET option, so you cannot get the result.

Notice that at the end of the second url output some of the url is missing which was still there using echo $LINK previously. The part dc3ba597-543d-48cd-935d-4e606920bdc0&name=htdocs.zip of the url is missing. I dont understand why that is the case if i use the same variable twice.

This issue cannot be reproduced, please recheck your script.

CodePudding user response:

I could get around the problem with a wget command.

wget $LINK -O htdocs.zip

The variable LINK seems to be faulty for the curl command but works just fine with wget. With the set -x command one can see, that there are ' inside the variable which shouldnt be there.

  • LINK='https://address.net/file/5ad2b26e-9aa3-462d-980c-323adde372d5/binary?token=eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJkYzNiYTU5Ny01NDNkLTQ4Y2QtOTM1ZC00ZTYwNjkyMGJkYzAiLCJhY2Nlc3MiOnsidXJuOmZpbGVzdG9yZTpmaWxlOjVhZDJiMjZlLTlhYTMtNDYyZC05ODBjLTMyM2FkZGUzNzJkNSI6WyJyZWFkIl19LCJleHAiOjE2NjczNzk3MDMsIm5iZiI6MTY2NzI5Njc4M30.o4wXDuamE2-5GNbF-i6tLWCu58WZzZ_iWW3YO40p4fU&client=dc3ba597-543d-48cd-935d-4e6'6920bdc0&name=htdocs.zip

Near the end of the link (close to the last -) there should be a zero instead of a ' . This was and is not visible with the command echo LINK which still would display the right link (without ' and with a zero at the last ').

CodePudding user response:

Don't use the single quotes in "'$LINK'". Just use

$ curl -v $LINK --user myMail.com:6N6usevDb7qDthqrsxJeE655 --output htdocs.zip 

Or

$ curl -v "$LINK" --user myMail.com:6N6usevDb7qDthqrsxJeE655 --output htdocs.zip

  • Related