Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux bash中的Curl请求,授权如何转义'';_Linux_Bash_Shell_Curl - Fatal编程技术网

Linux bash中的Curl请求,授权如何转义'';

Linux bash中的Curl请求,授权如何转义'';,linux,bash,shell,curl,Linux,Bash,Shell,Curl,我正在编写一个bash脚本来使用自定义头进行卷曲。请求看起来像 curl --location --request GET 'http://100.200.159.79:80/n_clone/' \ --header 'Authorization: Bearer 5e0333f03d311b646f84424a047abef08433d9ed' \ --header 'Content-Type: multipart/form-data; boundary=--------------------

我正在编写一个bash脚本来使用自定义头进行卷曲。请求看起来像

curl --location --request GET 'http://100.200.159.79:80/n_clone/' \
--header 'Authorization: Bearer 5e0333f03d311b646f84424a047abef08433d9ed' \
--header 'Content-Type: multipart/form-data; boundary=--------------------------905029954124550761104092'
我想在Bearer之后添加一个变量

access_token= "5e0af6333f03b646f84424a047abef08433d9ed77411b4f4"
auth_head= "'Authorization: Bearer'$access_to"

get_data=$(curl --location --request GET "http://100.200.159.79:80/n_clone/" \
-H "Authorization: Bearer $access_token" \
-H "Content-Type: multipart/form-data; boundary=--------------------------651618584466976849992097")
我也试过了

get_data=$(curl --location --request GET 'http://100.200.159.79:80/n_clone/' \
-H 'Authorization: Bearer '$access_token \
-H 'Content-Type: multipart/form-data; boundary=--------------------------651618584466976849992097')

如何在
'
(单引号)之间插入变量,以使curl请求正常工作?此时,它抛出一个Authorization字段丢失的错误。

只需对整个字符串使用双引号;授权头的定义中根本不需要单引号

access_token="5e0af6333f03b646f84424a047abef08433d9ed77411b4f4"
get_data=$(curl --location --request GET 'http://100.200.159.79:80/n_clone/' \
-H "Authorization: Bearer $access_token" \
-H 'Content-Type: multipart/form-data; boundary=--------------------------651618584466976849992097')
这里我们只需要shell使用的语法引号。当您将引号放在其他引号内时,它们将成为文本数据,传递给
curl
。如果远程服务器的头中不需要文字引号字符,我们就永远不想将文字引号传递给curl


顺便说一下,使用以下方法同样有效:

-H 'Authorization: Bearer '"$access_token" \

…其中,我们在单引号上下文中使用字符串
授权:Bearer
,然后在双引号上下文中扩展
$access\u token
;我没有在上面建议的原因是,
Authorization:Bearer
在单引号和双引号中以相同的方式展开,所以当您只能有一个上下文时,为什么要使用两个不同的上下文呢?

您不应该在标题中有任何文字引号。不要尝试注射它们。这里应该包含的唯一引号是语法引号,在shell计算出字符串应该如何分解为单个参数列表时,它会读取并删除这些引号。此外,赋值中
=
周围的空格是不允许的。顺便说一句,社区维基已经回答了这个问题,因为我相信这个问题是重复的,但是还没有找到先前存在的实例来标记它;我不想因为故意回答一个骗局而得到名誉积分。谢谢,我已经掌握了窍门。将问题标记为重复问题。