bash curl 302 重定向不起作用(命令行)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20905210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:35:06  来源:igfitidea点击:

curl 302 redirect not working (command line)

bashcurlscrape

提问by user2029890

In the browser, navigating to this URL initiates a 302 (moved temporarily) request which in turn downloads a file.

在浏览器中,导航到此 URL 会启动 302(临时移动)请求,该请求又会下载文件。

http://www.targetsite.com/target.php/?event=download&task_id=123

http://www.targetsite.com/target.php/?event=download&task_id=123

When I view what is actually happening via Chrome network tools I see that the redirect is going to a dynamically generated path that cancels itself immediately after download. In other words, even if I know that full path I will not have time to manually call it.

当我通过 Chrome 网络工具查看实际发生的事情时,我看到重定向将转到动态生成的路径,该路径在下载后立即取消。换句话说,即使我知道完整路径,我也没有时间手动调用它。

So, how in using the command line can I mimic the browser actions?

那么,如何使用命令行来模拟浏览器操作?

I tried

我试过

curl --cookies bin/cookies.txt -O -L "http://www.targetsite.com/target.php/?event=download&task_id=123" --compressed

curl --cookies bin/cookies.txt -O -L " http://www.targetsite.com/target.php/?event=download&task_id=123" --compressed

but this just returns gibberish. The goal of this is to programmatically download this file without having to navigate to the site. Unfortunately I cannot share the site here as it is behind a log-in.

但这只会返回胡言乱语。这样做的目的是以编程方式下载此文件,而无需导航到该站点。不幸的是,我无法在此处共享该站点,因为它需要登录。

回答by devnull

You need to supply the -Lor --locationoption in order to enable curlto follow HTTP redirects.

您需要提供-Lor--location选项才能启用curl跟踪 HTTP 重定向。

Quoting from man curl:

引自man curl

   -L, --location
          (HTTP/HTTPS)  If  the server reports that the requested page has
          moved to a different location (indicated with a Location: header
          and  a  3XX  response code), this option will make curl redo the
          request on the new place. If used together with -i, --include or
          -I, --head, headers from all requested pages will be shown. When
          authentication is used, curl only sends its credentials  to  the
          initial  host.  If a redirect takes curl to a different host, it
          won't be able to intercept the user+password. See  also  --loca‐
          tion-trusted  on how to change this. You can limit the amount of
          redirects to follow by using the --max-redirs option.

          When curl follows a redirect and the request is not a plain  GET
          (for example POST or PUT), it will do the following request with
          a GET if the HTTP response was 301, 302, or 303. If the response
          code  was  any  other  3xx code, curl will re-send the following
          request using the same unmodified method.