HTTPとは
HTTP(HyperText Transfer Protocol)完全ガイド
Section titled “HTTP(HyperText Transfer Protocol)完全ガイド”HTTPは、WebブラウザとWebサーバー間でデータをやり取りするためのプロトコルです。Webアプリケーション開発の基礎となる知識を詳しく解説します。
1. HTTPとは
Section titled “1. HTTPとは”HTTPの役割
Section titled “HTTPの役割”HTTP(HyperText Transfer Protocol)は、WebブラウザとWebサーバー間でHTML、画像、JSONなどのリソースを転送するためのプロトコルです。
クライアント(ブラウザ) ↓ HTTPリクエスト サーバー ↓ HTTPレスポンス クライアント(ブラウザ)HTTPの特徴
Section titled “HTTPの特徴”- ステートレス: 各リクエストは独立しており、前のリクエストの状態を保持しない
- リクエスト/レスポンスモデル: クライアントがリクエストを送信し、サーバーがレスポンスを返す
- テキストベース: 人間が読める形式で通信
2. HTTPリクエスト
Section titled “2. HTTPリクエスト”リクエストの構造
Section titled “リクエストの構造”GET /index.html HTTP/1.1Host: example.comUser-Agent: Mozilla/5.0...Accept: text/html,application/xhtml+xmlAccept-Language: ja,en-US;q=0.9Connection: keep-aliveHTTPメソッド
Section titled “HTTPメソッド”GET /api/users HTTP/1.1Host: api.example.com- リソースの取得
- パラメータはURLに含める
- キャッシュ可能
POST /api/users HTTP/1.1Host: api.example.comContent-Type: application/json
{ "name": "John Doe", "email": "john@example.com"}- リソースの作成
- データはリクエストボディに含める
- キャッシュ不可
PUT /api/users/1 HTTP/1.1Host: api.example.comContent-Type: application/json
{ "name": "Jane Doe", "email": "jane@example.com"}- リソースの更新(完全置換)
PATCH /api/users/1 HTTP/1.1Host: api.example.comContent-Type: application/json
{ "email": "newemail@example.com"}- リソースの部分更新
DELETE
Section titled “DELETE”DELETE /api/users/1 HTTP/1.1Host: api.example.com- リソースの削除
リクエストヘッダー
Section titled “リクエストヘッダー”GET /index.html HTTP/1.1Host: example.com # ホスト名User-Agent: Mozilla/5.0... # クライアント情報Accept: text/html,application/json # 受け入れ可能なコンテンツタイプAccept-Language: ja,en-US;q=0.9 # 言語設定Accept-Encoding: gzip, deflate # 圧縮形式Connection: keep-alive # 接続の維持Cookie: session_id=abc123 # クッキーAuthorization: Bearer token123 # 認証情報3. HTTPレスポンス
Section titled “3. HTTPレスポンス”レスポンスの構造
Section titled “レスポンスの構造”HTTP/1.1 200 OKContent-Type: text/html; charset=utf-8Content-Length: 1234Date: Mon, 01 Jan 2024 12:00:00 GMTServer: nginx/1.18.0
<!DOCTYPE html><html>...</html>HTTPステータスコード
Section titled “HTTPステータスコード”2xx(成功)
Section titled “2xx(成功)”200 OK: リクエスト成功201 Created: リソース作成成功204 No Content: リクエスト成功(コンテンツなし)3xx(リダイレクト)
Section titled “3xx(リダイレクト)”301 Moved Permanently: 恒久的なリダイレクト302 Found: 一時的なリダイレクト304 Not Modified: キャッシュが有効4xx(クライアントエラー)
Section titled “4xx(クライアントエラー)”400 Bad Request: 不正なリクエスト401 Unauthorized: 認証が必要403 Forbidden: アクセス拒否404 Not Found: リソースが見つからない5xx(サーバーエラー)
Section titled “5xx(サーバーエラー)”500 Internal Server Error: サーバー内部エラー502 Bad Gateway: ゲートウェイエラー503 Service Unavailable: サービス利用不可504 Gateway Timeout: ゲートウェイタイムアウトレスポンスヘッダー
Section titled “レスポンスヘッダー”HTTP/1.1 200 OKContent-Type: text/html; charset=utf-8 # コンテンツタイプContent-Length: 1234 # コンテンツサイズDate: Mon, 01 Jan 2024 12:00:00 GMT # レスポンス日時Server: nginx/1.18.0 # サーバー情報Cache-Control: max-age=3600 # キャッシュ制御Set-Cookie: session_id=abc123 # クッキーの設定Location: https://example.com/new # リダイレクト先4. HTTP/1.1の特徴
Section titled “4. HTTP/1.1の特徴”持続的接続(Keep-Alive)
Section titled “持続的接続(Keep-Alive)”Connection: keep-alive- 1つのTCP接続で複数のリクエスト/レスポンスを処理
- 接続の確立コストを削減
パイプライン
Section titled “パイプライン”- 複数のリクエストを順次送信(レスポンスを待たない)
- 実装が複雑で、あまり使われていない
チャンク転送
Section titled “チャンク転送”Transfer-Encoding: chunked- 大きなファイルを小さなチャンクに分割して転送
- ストリーミング配信に使用
5. HTTP/2の特徴
Section titled “5. HTTP/2の特徴”マルチプレクシング
Section titled “マルチプレクシング”- 1つのTCP接続で複数のリクエスト/レスポンスを並列処理
- HTTP/1.1のパイプラインの問題を解決
サーバープッシュ
Section titled “サーバープッシュ”- サーバーがクライアントにリソースを事前に送信
- ページ読み込みの高速化
ヘッダー圧縮
Section titled “ヘッダー圧縮”- HPACKアルゴリズムでヘッダーを圧縮
- 通信量の削減
バイナリプロトコル
Section titled “バイナリプロトコル”- テキストベースからバイナリベースに変更
- パースの高速化
6. HTTP/3の特徴
Section titled “6. HTTP/3の特徴”QUICプロトコル
Section titled “QUICプロトコル”- UDPベースのトランスポートプロトコル
- TCPの3ウェイハンドシェイクを省略
- 接続の高速化
マルチストリーム
Section titled “マルチストリーム”- 複数のストリームを独立して処理
- 1つのストリームがブロックされても、他のストリームは影響を受けない
7. 実務でのHTTP活用
Section titled “7. 実務でのHTTP活用”curlコマンドでのHTTPリクエスト
Section titled “curlコマンドでのHTTPリクエスト”# GETリクエストcurl https://example.com
# POSTリクエストcurl -X POST https://api.example.com/users \ -H "Content-Type: application/json" \ -d '{"name": "John Doe", "email": "john@example.com"}'
# ヘッダーの確認curl -I https://example.com
# 詳細な情報の表示curl -v https://example.comブラウザの開発者ツール
Section titled “ブラウザの開発者ツール”- Networkタブを開く(F12)
- リクエストを選択
- Headersタブで確認
- Request Headers: 送信されたヘッダー
- Response Headers: 受信したヘッダー
- Status Code: HTTPステータスコード
8. よくある問題と解決策
Section titled “8. よくある問題と解決策”問題1: 404エラー
Section titled “問題1: 404エラー”原因:
- URLが間違っている
- リソースが存在しない
解決策:
- URLを確認
- サーバーのログを確認
- ルーティング設定を確認
問題2: CORSエラー
Section titled “問題2: CORSエラー”原因:
- クロスオリジンリクエストがブロックされている
解決策:
Access-Control-Allow-Origin: *Access-Control-Allow-Methods: GET, POST, PUT, DELETEAccess-Control-Allow-Headers: Content-Type, Authorization問題3: リクエストが遅い
Section titled “問題3: リクエストが遅い”原因:
- ネットワークの問題
- サーバーの負荷
- リクエストサイズが大きい
解決策:
- HTTP/2の使用
- リクエストの最適化
- キャッシュの活用
これで、HTTPの仕組みと実務での使い方を理解できるようになりました。