为什么 curl return 文件名而不是文件内容?
Why does curl return file name instead of file contents?
我正在尝试在 shell 中使用 curl 从服务器检索文件并进行身份验证。当我使用 Web 浏览器(以及一些 REST 客户端)发出请求时,我得到了文件内容。对于其他 REST 客户端,以及 curl 和 wget,我只得到文件名作为响应。
很遗憾,由于我的工作原因,我无法提供可重现的示例。
当我使用浏览器发出请求时,我将我的登录信息键入并输入到:
www.domain.com/path/to/api?path=/filepath/file.ext
这总是有效,我下载了文件 - 我已经在 Opera、Firefox 和 Chrome 中试过了。同样,对于 Firefox RESTClient 扩展,我使用这些设置:
Method: GET
URL: www.domain.com/path/to/api?path=/filepath/file.ext
Authorization: Basic blablabla
这总是有效,我得到了文件的内容作为响应。
然而,当我在 Chrome 的 Advanced Rest Client 上使用(我确定是)相同的设置时,我只是得到文件名作为响应。
Method: GET
URL: https://www.domain.com/path/to/api?path=/filepath/file.ext
Authorization: Basic blablabla
我知道授权不是问题 - 当我输入不正确的信息时,我会收到回复告诉我。 URL 是相同的 - 我只是在 windows 之间复制和粘贴。 Firefox 的 RESTClient 必须添加一些 Chrome 的 ARC 没有的附加选项。不幸的是,我不知道如何查看客户端发送的原始请求 - 能够看到它们可能会让我解决我的问题。
在 shell 中,我遇到了相同的行为
$ curl --user user:password https://www.domain.com/path/to/api?path=/path/file.ext
"/path/file.ext"
当我使用 --raw 选项时,我得到的输出略有不同:
$ curl --user user:password https://www.domain.com/path/to/api?path=/path/file.ext --raw
10
"/path/file.ext"
0
控制台总是在 0 之后打印 2 个额外的空行。第一个数字总是与文件扩展名之前的字符数完全匹配,第二个数字总是 0。0 是否意味着 curl 正在接收一个空文件?
使用 wget,我得到了相同的行为:
$ wget --user user --password password https://www.domain.com/path/to/api?path=/path/file.ext
--[[Time Stamp]]-- https://www.domain.com/path/to/api?path=/filepath/file.ext
Resolving www.domain.com (www.domain.com)... 0.0.0.0
Connecting to www.domain.com (www.domain.com)|0.0.0.0|:443... connected.
HTTP request sent, awaiting response... 401 Processed
Authentication selected: Basic realm="Basic realm"
Reusing existing connection to www.domain.com:443.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/json]
Saving to: ‘files@path=%2Fpath%2Ffile.ext’
0K [[Download Speed]]=[[Download Time]]
[[Time Stamp]] ([[Download Speed]]) - ‘files@path=%2Fpath%2Ffile.ext’ saved [22]
文件'files@path=%2Fpath%2Ffile.ext'的内容只是
"/path/file.ext"
有人建议我尝试 -qO
,因为 -q
关闭控制台输出并且 -O
将输出写入文件。生成的文件仅包含文件名。
$ wget --user user --password password -qO file.ext https://www.domain.com/path/to/api?path=/path/file.ext
$ cat file.ext
"/path/file.ext"
引号包含在文件内容中。我也尝试使用 --ask-password
而不是 --password
并收到相同的结果。
我的猜测是 curl 有一些选项我丢失或没有正确使用。我已经尝试了 -L, --location
和 this documentation 中的一些其他方法,因为这看起来最像我正在尝试做的事情,但它不会以任何方式改变输出。
原来服务器正在寻找一个请求 header。服务器认为我不想接受它拥有的文件类型。我需要问它:
curl --user user:password https://www.domain.com/path/to/api?path=/filepath/file.ext -H "Accept: application/app"
我正在尝试在 shell 中使用 curl 从服务器检索文件并进行身份验证。当我使用 Web 浏览器(以及一些 REST 客户端)发出请求时,我得到了文件内容。对于其他 REST 客户端,以及 curl 和 wget,我只得到文件名作为响应。
很遗憾,由于我的工作原因,我无法提供可重现的示例。
当我使用浏览器发出请求时,我将我的登录信息键入并输入到:
www.domain.com/path/to/api?path=/filepath/file.ext
这总是有效,我下载了文件 - 我已经在 Opera、Firefox 和 Chrome 中试过了。同样,对于 Firefox RESTClient 扩展,我使用这些设置:
Method: GET
URL: www.domain.com/path/to/api?path=/filepath/file.ext
Authorization: Basic blablabla
这总是有效,我得到了文件的内容作为响应。
然而,当我在 Chrome 的 Advanced Rest Client 上使用(我确定是)相同的设置时,我只是得到文件名作为响应。
Method: GET
URL: https://www.domain.com/path/to/api?path=/filepath/file.ext
Authorization: Basic blablabla
我知道授权不是问题 - 当我输入不正确的信息时,我会收到回复告诉我。 URL 是相同的 - 我只是在 windows 之间复制和粘贴。 Firefox 的 RESTClient 必须添加一些 Chrome 的 ARC 没有的附加选项。不幸的是,我不知道如何查看客户端发送的原始请求 - 能够看到它们可能会让我解决我的问题。
在 shell 中,我遇到了相同的行为
$ curl --user user:password https://www.domain.com/path/to/api?path=/path/file.ext
"/path/file.ext"
当我使用 --raw 选项时,我得到的输出略有不同:
$ curl --user user:password https://www.domain.com/path/to/api?path=/path/file.ext --raw
10
"/path/file.ext"
0
控制台总是在 0 之后打印 2 个额外的空行。第一个数字总是与文件扩展名之前的字符数完全匹配,第二个数字总是 0。0 是否意味着 curl 正在接收一个空文件?
使用 wget,我得到了相同的行为:
$ wget --user user --password password https://www.domain.com/path/to/api?path=/path/file.ext
--[[Time Stamp]]-- https://www.domain.com/path/to/api?path=/filepath/file.ext
Resolving www.domain.com (www.domain.com)... 0.0.0.0
Connecting to www.domain.com (www.domain.com)|0.0.0.0|:443... connected.
HTTP request sent, awaiting response... 401 Processed
Authentication selected: Basic realm="Basic realm"
Reusing existing connection to www.domain.com:443.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/json]
Saving to: ‘files@path=%2Fpath%2Ffile.ext’
0K [[Download Speed]]=[[Download Time]]
[[Time Stamp]] ([[Download Speed]]) - ‘files@path=%2Fpath%2Ffile.ext’ saved [22]
文件'files@path=%2Fpath%2Ffile.ext'的内容只是
"/path/file.ext"
有人建议我尝试 -qO
,因为 -q
关闭控制台输出并且 -O
将输出写入文件。生成的文件仅包含文件名。
$ wget --user user --password password -qO file.ext https://www.domain.com/path/to/api?path=/path/file.ext
$ cat file.ext
"/path/file.ext"
引号包含在文件内容中。我也尝试使用 --ask-password
而不是 --password
并收到相同的结果。
我的猜测是 curl 有一些选项我丢失或没有正确使用。我已经尝试了 -L, --location
和 this documentation 中的一些其他方法,因为这看起来最像我正在尝试做的事情,但它不会以任何方式改变输出。
原来服务器正在寻找一个请求 header。服务器认为我不想接受它拥有的文件类型。我需要问它:
curl --user user:password https://www.domain.com/path/to/api?path=/filepath/file.ext -H "Accept: application/app"