从脚本调用时,curl 返回 HTTP 代码 503

Pie*_*reF 2 bash curl

我尝试调用一些 REST API 并使用curl. 如果在终端中输入:

curl -s -o /dev/null -I -w '%{http_code}' -X POST 'http://localhost/gitlab/api/v3/projects?private_token=my_private_token&name=blabla' -H 'Content-Length: 0'
Run Code Online (Sandbox Code Playgroud)

它工作并返回 HTTP 代码201(“创建”)。现在我尝试在 bash 脚本中使用这个命令,用变量替换一部分 url:

echo "Enter base URL :"
read gitlab_url # Here I type 'http://localhost/gitlab', to generate the same URL as in first code snippet
code_status=$(curl -s -o /dev/null -I -w '%{http_code}' -X POST '$gitlab_url/api/v3/projects?private_token=my_private_token&name=blabla' -H 'Content-Length: 0')
echo "$code_status"
Run Code Online (Sandbox Code Playgroud)

然后它返回给我 HTTP 代码503(“服务不可用”)。要查看“硬编码” URL 和生成的 URL 之间是否存在任何差异,我执行以下操作:

echo "curl -s -o /dev/null -I -w '%{http_code}' -X POST '$gitlab_url/api/v3/projects?private_token=my_private_token&name=blabla' -H 'Content-Length: 0'"
# Output :
curl -s -o /dev/null -I -w '%{http_code}' -X POST 'http://localhost/gitlab/api/v3/projects?private_token=my_private_token&name=blabla' 'Content-Length: 0'
Run Code Online (Sandbox Code Playgroud)

如果我直接在终端中执行它,它会工作并返回 me 201。所以:如果我在脚本中使用它,为什么这个命令会失败?有什么我错过的吗?

Pie*_*reF 5

这是一个代理问题。如果我使用,curl -v ....我可以看到以下输出:

什么时候curl直接在终端中输入我有:

* About to connect() to localhost port 80 (#0)
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 80 (#0)
Run Code Online (Sandbox Code Playgroud)

当我将它用于 bash 脚本时,我得到:

* About to connect() to proxy proxy.my.company port xxx (#0)
*   Trying xx.xx.xx.xx... connected
* Connected to proxy.my.company (xx.xx.xx.xx) port xxx (#0)
Run Code Online (Sandbox Code Playgroud)

所以为了修复它,我在脚本的顶部添加了这个:

export no_proxy=localhost,127.0.0.1
export http_proxy=""
Run Code Online (Sandbox Code Playgroud)

我很惊讶必须这样做,因为我已经有一个环境变量no_proxy已经引用localhost127.0.0.1