本文介绍了在 bash 脚本中使用 curl 并获取 curl:(3) 在 URL 中发现非法字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个非常简单的 bash 脚本,它可以卷曲到身份验证服务器以获取标头.标头 url 被写入一个 var,然后在下一个 curl 调用中使用.在第一个 curl 调用中使用 var 设置时,我收到curl: (3) Illegal characters found in URL".我能够回显 var 并且一切看起来都不错,我什至可以重置 var(在下面的示例中)并且它可以工作.

So I have a very simple bash script that is curl'ing to an auth server for a header. The header url is written to a var and then used in the next curl call. When using the var set in the first curl call I am getting "curl: (3) Illegal characters found in URL". I am able to echo the var and all looks good, I am even able to reset the var (in my example below) and it works.

Bash 脚本

URL=$(curl -i -X GET -H "X-Auth-User: MyUserna,e" -H "X-Auth-Key: MyAPIKey" "https://urlToAuthServer.tld/auth/v1.0/" | grep "X-Storage-Url:" | awk '{print $2}')

curl -X GET -H "X-Auth-Token: MyAuthTok" "${URL}/folder/myfile.txt" -o ./myfile.txt

运行上面的例子时,我得到:

curl: (3) Illegal characters found in URL

URL var 看起来像这样(没有非法字符)

https://somesecureurl.com/auth/AUTH_67383834-45245453-g34g34t5-34534

当我在终端中执行此操作时,它会起作用:

$ URL=$(curl -i -X GET -H "X-Auth-User: MyUserna,e" -H "X-Auth-Key: MyAPIKey" "https://urlToAuthServer.tld/auth/v1.0/" | grep "X-Storage-Url:" | awk '{print $2}')

$ echo $URL

$ echo $URL

https://somesecureurl.com/auth/AUTH_67383834-45245453-g543-g34/a>

https://somesecureurl.com/auth/AUTH_67383834-45245453-g34g34t5-34534

现在我复制并粘贴字符串并将其重新分配到 URL 像这样(再次全部在终端中):

Now I copy and paste the string and reasign it to URL like so (again all in terminal):

>$ URL="https://somesecureurl.com/auth/AUTH_67383834-45245453-g34g34t5-34534"
>$ curl -X GET -H "X-Auth-Token: MyAuthTok" "${URL}/folder/myfile.txt" -o ./myfile.txt

它有效.

那么为什么我会在第一个示例中收到curl: (3) Illegal characters found in URL"错误?

So why am I getting the "curl: (3) Illegal characters found in URL" error in the first example?

更新我跑了这个:printf %s "$URL" |xxd

这是输出(地址已更改,您明白了)

Here is the output (addressed changed up you get the idea)

0000000: 6874 7470 733a 2f2f 6461 6c30 352e 6f62  https://server.ob
0000010: 6a65 6374 7374 6f72 6167 652e 736f 6674  jectstorage.lite
0000020: 6c61 7965 722e 6e65 742f 7631 2f41 5554  sabers.com/v1/AUT
0000030: 485f 6665 3235 3339 3434 2d38 6433 322d  H_aE2563981-7d32-
0000040: 3432 3138 2d61 6566 632d 6665 6638 3465  4201-bdoi-fef94a
0000050: 6166 3331 6232 0d                        ag11c8.

推荐答案

$URL 在末尾 (0d) 包含一个 (CR).用

The $URL contains a (CR) at the end (0d). Remove it with

URL=${URL%$'
'}

在与 curl 一起使用之前.

before using it with curl.

这篇关于在 bash 脚本中使用 curl 并获取 curl:(3) 在 URL 中发现非法字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

查看更多