HTTPヘッダ
HTTPヘッダとは次のようなものです。RequestとResponseで使うパラメータが若干違います。
GET /home.html HTTP/1.1
Host: developer.mozilla.org
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://developer.mozilla.org/testpage.html
Connection: keep-alive
Upgrade-Insecure-Requests: 1
If-Modified-Since: Mon, 18 Jul 2016 02:36:04 GMT
If-None-Match: "c561c68d0ba92bbeb8b0fff2a9199f722e3a621a"
Cache-Control: max-age=0
例引用
200 OK
Access-Control-Allow-Origin: *
Connection: Keep-Alive
Content-Encoding: gzip
Content-Type: text/html; charset=utf-8
Date: Mon, 18 Jul 2016 16:06:00 GMT
Etag: "c561c68d0ba92bbeb8b0f612a9199f722e3a621a"
Keep-Alive: timeout=5, max=997
Last-Modified: Mon, 18 Jul 2016 02:36:04 GMT
Server: Apache
Set-Cookie: mykey=myvalue; expires=Mon, 17-Jul-2017 16:06:00 GMT; Max-Age=31449600; Path=/; secure
Transfer-Encoding: chunked
Vary: Cookie, Accept-Encoding
X-Backend-Server: developer2.webapp.scl3.mozilla.com
X-Cache-Info: not cacheable; meta data too large
X-kuma-revision: 1085259
x-frame-options: DENY
例引用
よく使われるHTTPヘッダ
Date
日時のデータを持つヘッダです。HTTPでは電子メールのフォーマットを使用しています。
Date: Mon, 17 May 2020 11:06:49 GMT
Responseで使われるDate
- If-Modified-Since
- If-Unmodified-Since
どちらもリクエストを条件付にします。
If-Modified-Since
はGETメソッドでリソースの更新日時を指定する場合に使います。
If-Unmodified-Since
はPUTやDELETEメソッド等でリソースの更新日時を指定する場合に使います。
Request
- Expires
- Last-Modified
- Retry-After
Expries
はキャッシュできる時間を指定します。
Last-Modified
はリソースを最後に更新した時間が入ります。
Retry-After
はもう一度Requestを送信できる日時が入ります。
MIMEメディアタイプ
Content-Type
ボディに入っているコンテンツが何なのかを指定します。
- application
- audio
- example
- font
- model
- text
- video
Content-Type: text/html;
の右のように、書き方はタイプ/サブタイプとなります。
今回の例で言えば、textが入っていて、その中身はhtmlだよというのを示しています。
charset
コンテンツの文字フォーマットが何でエンコードしているのかを指定します。
Content-Type: text/html; charset=utf-8
このパラメータは省略可能なのですが、もしContent-Typeがtextを指定していて、日本語を扱う場合は注意が必要です。HTTPでは、Content-TypeがtextのものはISO 8859-1と定義されており、英語以外で書かれた文書はcharsetを省くと文字化けを起こしてしまいます。
言語タグ
コンテンツのエンコードを示すものはcharsetでしたが、コンテンツのリソース表現の自然言語を示すものもあります。
Content-Language: ja-JP
コンテンツネゴシエーション
クライアントが処理できるContent-Typeで指定できるコンテンツを、サーバー側へ伝えるものです。
Accept
Accept: text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8
Acceptはクライアントが処理できるコンテンツを羅列できます
送られたサーバーは、Acceptから一つを選択し、Responseを返します。
Content-Length
Bodyの長さを指定します。
10進数のバイトで表記できます。
Content-Length: 256
Transfer-Encoding
bodyを分割して送ることができます。
Transfer-Encoding: chunked
Transfer-Encoding: compress
Transfer-Encoding: deflate
Transfer-Encoding: gzip
Transfer-Encoding: identity
例引用