0
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?

【学習】「HTTPリクエストのインスタンス」について学ぶ

0
Posted at

結論

HTTPリクエストのインスタンスとは、
HTTPリクエストの情報をひとまとめにした「リクエスト用オブジェクト」のことである。

1. HTTPリクエストとは

フロントエンドから、

GET https://api.example.com/users/123

にアクセスするとき、サーバーには以下のような情報を送っている。

HTTPリクエスト
├─ Method: GET
├─ URL: https://api.example.com/users/123
├─ Headers
│   ├─ Authorization: Bearer xxx
│   └─ Content-Type: application/json
└─ Body
    └─ なし

POSTの場合は、

POST https://api.example.com/users

Headers:
  Content-Type: application/json

Body:
{
  "name": "Taro",
  "age": 30
}

のようになる。

つまり、HTTPリクエストとは単なるURLではなく、
「どこに、何を、どんな条件で送るのか」
という情報の集合である。

2. HTTPリクエストを表すオブジェクト

HTTPリクエストの情報は、プログラム上のオブジェクトとして扱うことができる。

例えばFetch APIでは、HTTPリクエストを表すRequestというクラスが用意されている。

const request = new Request(
  "https://api.example.com/users",
  {
    method: "GET",
    headers: {
      Authorization: "Bearer xxx",
    },
  }
);

このrequestは、1回分のHTTPリクエストを表す。

3. インスタンスとは

インスタンスとは、一般的には「クラスや設計図などをもとに、実際に作られた具体的なオブジェクト」のことを表す。

「HTTPリクエストのインスタンス」とは、今回の例でいうと、

const request = new Request(...);

Requestクラスをもとにして作られた、このrequestオブジェクトのことである。

作成したrequestは、

const response = await fetch(request);

のように、fetch()に渡して送信することができる。

0
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
0
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?