36
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[小ネタ]curl で3xxの時にリダイレクトする

Posted at

-Lオプションを付与すればOK.
レスポンスでスタータスコードが3xxでLocationヘッダーにURLが指定されてる場合に、Locationヘッダーに指定されたURLへのリクエストも実施します。

# -L オプションを付けないとリダイレクトしない
$curl -v https://google.co.jp
* Rebuilt URL to: https://google.co.jp/
*   Trying 172.217.26.3...
* Connected to google.co.jp (172.217.26.3) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate:
*       subject: CN=*.google.co.jp,O=Google Inc,L=Mountain View,ST=California,C=US
*       start date:  9月 13 17:11:49 2017 GMT
*       expire date: 12月 06 17:09:00 2017 GMT
*       common name: *.google.co.jp
*       issuer: CN=Google Internet Authority G2,O=Google Inc,C=US
> GET / HTTP/1.1
> User-Agent: curl/7.40.0
> Host: google.co.jp
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Location: https://www.google.co.jp/
< Content-Type: text/html; charset=UTF-8
< Date: Sun, 01 Oct 2017 12:20:42 GMT
< Expires: Tue, 31 Oct 2017 12:20:42 GMT
< Cache-Control: public, max-age=2592000
< Server: gws
< Content-Length: 222
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
< Alt-Svc: quic=":443"; ma=2592000; v="39,38,37,35"
<
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.co.jp/">here</A>.
</BODY></HTML>
* Connection #0 to host google.co.jp left intact

# -Lオプション付与でリダイレクト
$curl -L https://google.co.jp

なお、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.

              You can tell curl to not change the non-GET  request  method  to
              GET  after  a  30x  response  by using the dedicated options for
              that: --post301, --post302 and -post303.
36
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
36
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?