【问题标题】:Why does curl return file name instead of file contents?为什么 curl 返回文件名而不是文件内容?
【发布时间】:2015-01-12 17:24:31
【问题描述】:

我正在尝试在 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 是相同的 - 我只是在窗口之间复制和粘贴。 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, --locationthis documentation 的其他几个,因为这似乎最像我想要做的,但它不会以任何方式改变输出。

【问题讨论】:

  • 试试wget --user user --password password -qO - http://www.address.com/path/to/file。也尝试一个没有密码的网站,这可能是一个权限问题。在旁注中,wget 输出到一个文件(使用它显示的名称),所以也试试cat file.ext,在你调用wget 的目录中。
  • 谢谢,但没有骰子。结果文件只包含引用的文件路径。 wget google.com 工作得很好。 wget http://mirrors.kernel.org/gnu/make/make-3.75-3.76.diff.gz 也是如此。我将不得不询问有关权限的问题 - 因为 API 是为我们设计的,所以我们应该有权限。但可能有人在某个地方搞砸了。
  • 你试过wget --ask-password ...吗?
  • 您是否在这些不同的情况下转储了实际的请求标头? curl 和 wget 当然可以做到这一点。我愿意相信 firefox 和 chrome 工具也可以做到这一点。
  • 另一种“解决” curl 请求的好方法是转储响应标头(使用 -D 标记)——它可以为您提供 HTTP 状态代码和其他有用的信息。

标签: shell rest curl


【解决方案1】:

原来服务器正在寻找请求标头。服务器认为我不想接受它拥有的文件类型。我需要问它:

curl --user user:password https://www.domain.com/path/to/api?path=/filepath/file.ext -H "Accept: application/app"

【讨论】:

  • 啊,是的,mime-types 是我的下一个最佳猜测:D
  • 很好,很高兴你修复了它!接受你自己的答案,以帮助未来的人们!
  • 我现在愿意,但我需要 2 天才能接受我自己的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-02
  • 1970-01-01
  • 2016-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多