コンテンツにスキップ

JavaScript/Request

出典: フリー教科書『ウィキブックス(Wikibooks)』

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など)。
headersRequestオブジェクトとしてヘッダー情報を取得する。
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.arguments
Request.caller
Request.length
Request.name
Request.prototype

静的アクセサ

[編集]

静的メソッド

[編集]

Requestのインスタンスプロパティ

[編集]
Request.prototype [ Symbol.toStringTag ]

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

Requestのインスタンスメソッド

[編集]
Request.prototype.arrayBuffer()
Request.prototype.blob()
Request.prototype.bytes()
Request.prototype.clone()
Request.prototype.constructor()
Request.prototype.formData()
Request.prototype.json()
Request.prototype.text()
close