JavaScript/Request
表示
Requestオブジェクトとその活用
[編集]1. Requestオブジェクトとは
[編集]JavaScriptのRequest
オブジェクトは、Fetch API
を使用する際にHTTPリクエストの詳細を管理するためのオブジェクトです。このオブジェクトを利用することで、リクエストのURL、メソッド、ヘッダー、ボディなどを細かく設定することができます。
例えば、新しいRequest
オブジェクトを作成するには、以下のように記述します。
constrequest=newRequest("https://api.example.com/data",{method:"GET",headers:newRequest({"Accept":"application/json"}),cache:"no-cache"});console.log(request.url);// "https://api.example.com/data"console.log(request.method);// "GET"console.log(request.headers.get("Accept"));// "application/json"
この例では、指定されたURLに対してGET
メソッドを使用し、ヘッダーとしてAccept: application/json
を設定しています。
2. Requestオブジェクトのプロパティ
[編集]Request
オブジェクトには、リクエストの詳細を取得できるさまざまなプロパティが用意されています。
プロパティ名 | 説明 |
---|---|
url | リクエスト先のURLを取得する。 |
method | リクエストのHTTPメソッドを取得する(GET, POSTなど)。 |
headers | Request オブジェクトとしてヘッダー情報を取得する。 |
bodyUsed | ボディがすでに使用されたかを示すブール値。 |
cache | キャッシュモード("default"、"no-cache"など)を取得する。 |
redirect | リダイレクトポリシー("follow"、"error"、"manual")を取得する。 |
以下のコードは、Request
オブジェクトの主要プロパティの使用例です。
constrequest=newRequest("https://api.example.com/data",{method:"POST",headers:newRequest({"Content-Type":"application/json"}),body:JSON.stringify({key:"value"})});console.log(request.method);// "POST"console.log(request.headers.get("Content-Type"));// "application/json"console.log(request.bodyUsed);// false
3. Requestオブジェクトを使用したfetchの実行
[編集]Request
オブジェクトは、fetch
の引数として渡すことができます。これにより、より構造化された方法でHTTPリクエストを管理できます。
constrequest=newRequest("https://api.example.com/data",{method:"GET",headers:newRequest({"Accept":"application/json"})});fetch(request).then(response=>response.json()).then(data=>console.log(data)).catch(error=>console.error("Error:",error));
この例では、事前に作成したRequest
オブジェクトをfetch
に渡し、サーバーからJSONデータを取得しています。
4. Requestオブジェクトのコピー
[編集]Request
オブジェクトは、一度作成するとそのプロパティを変更できません。しかし、Request
オブジェクトを基に新しいリクエストを作成することは可能です。
constoriginalRequest=newRequest("https://api.example.com/data",{method:"GET",headers:newRequest({"Accept":"application/json"})});constnewRequest=newRequest(originalRequest,{method:"POST"});console.log(newRequest.method);// "POST"console.log(newRequest.url);// "https://api.example.com/data"
この方法を利用すると、既存のリクエストを基に、異なるメソッドやヘッダーを持つ新しいリクエストを作成できます。
5. まとめ
[編集]JavaScriptのRequest
オブジェクトは、HTTPリクエストを柔軟に管理するための強力な機能を提供します。fetch
と組み合わせることで、API通信の詳細を整理しやすくなります。適切に活用し、効率的なリクエスト管理を行いましょう。
附録
[編集]静的プロパティ
[編集]静的アクセサ
[編集]静的メソッド
[編集]Requestのインスタンスプロパティ
[編集]Requestのインスタンスアクセサ
[編集]- get Request.prototype.body
- get Request.prototype.bodyUsed
- get Request.prototype.cache
- get Request.prototype.credentials
- get Request.prototype.destination
- get Request.prototype.duplex
- get Request.prototype.headers
- get Request.prototype.integrity
- get Request.prototype.isHistoryNavigation
- get Request.prototype.keepalive
- get Request.prototype.method
- get Request.prototype.mode
- get Request.prototype.redirect
- get Request.prototype.referrer
- get Request.prototype.referrerPolicy
- get Request.prototype.signal
- get Request.prototype.targetAddressSpace
- get Request.prototype.url