2
0

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でHTTPコンテンツを部分的にダウンロードする

Posted at

経緯

巨大なファイルの形式チェックをするために、先頭から一定のバイト数だけダウンロードして確認したくなった。

cURLで部分ダウンロードする

cURLのオプションに以下があるのでそれを活用して部分ダウンロードできます。

 -r, --range RANGE   Retrieve only the bytes within a range

例:先頭バイト(0バイト目)から255バイト目までをダウンロードする

$ curl -r 0-255 "http://yourhost.com/huge_content.html"

こちらの形式は当然ながらRangeヘッダーに対応しているWebサーバー/コンテンツのみ有効です。
たとえばPHPなどで記述されているWebページの場合など、Rangeヘッダーを正しく送信しても206レスポンスを返さないケースもありますので注意が必要です。

Rangeヘッダーと応答レスポンスのサンプル

$ curl -v -r 0-100 "http://yourhost.com/huge_contet.html"

* 送信したヘッダ *
> GET /huge_content.html HTTP/1.1
> Range: bytes=0-100
> User-Agent: curl/7.35.0
> Host: yourhost.com
> Accept: */*
>

* 受信したヘッダ *
< HTTP/1.1 206 Partial Content
< Date: Wed, 18 Oct 2017 09:35:45 GMT
< Server: Apache
< Last-Modified: Tue, 17 Oct 2017 12:28:30 GMT
< Accept-Ranges: bytes
< Content-Length: 101
< Content-Range: bytes 0-100/1234567890
< Connection: close
< Content-Type: text/html
2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?