# ネットワークリクエストの送信

## `fetch`を使ってネットワークリクエストを送信する <a href="#vrr9c" id="vrr9c"></a>

`fetch`関数は、ネットワークリソースにアクセスするためのツールで、HTTPリクエストを送信し、リクエストのレスポンスを処理できます。`fetch`関数は、`GET`、`POST`、`PUT`、`DELETE`など、さまざまなタイプのリクエストに対応し、リクエスト本文とレスポンス本文のカスタムフォーマットにも対応しています。

`fetch`関数を使用すると、ネットワークリソースに簡単にアクセスし、リクエストとレスポンスのフローを制御できます。例えば、以下のコードを使用して`GET`リクエストを送信し、リクエストが完了した後にレスポンスを処理できます。

```javascript
fetch('https://example.com/api/endpoint')
    .then(response => response.json())
    .then(data => {
    	// ここでレスポンスを処理する
    });
```

この例では、指定されたネットワークリソースにGETリクエストを送信し、リクエストが完了した後にレスポンス本文をJSON形式に解析し、ここで解析されたレスポンス本文を処理します。

{% hint style="info" %}
JavaScript の `fetch` 関数について学ぶには、MDN Web Docs の以下のページを参照してください。\
<https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch>。

この記事では、`fetch`関数の基本的な使い方と、HTTPリクエストを送信し、リクエストのレスポンスを処理する方法を示すサンプルコードが紹介されています。

また、以下の記事も fetch についての詳細を知るために参考になります:

* "Using Fetch" (<https://davidwalsh.name/fetch>)
* "Fetch API In Depth" (<https://css-tricks.com/using-fetch/>)
  {% endhint %}

***

## `https` を使ってリクエストを送信する <a href="#oowx7" id="oowx7"></a>

ブラウザのデフォルトのセキュリティ制限のため、`fetch` メソッドには制限があることがあります。そのような場合、Node.js のネイティブなネットワーク API を使ってネットワークリクエストを送信した方が、より柔軟性があります。

`https.get`メソッドを使ってHTTP GETリクエストを送信するのは簡単で、リクエストのURLを指定するだけです。例えば、以下のコードでHTTP GETリクエストを送信できます。

```javascript
const https = require('https');

https.get('https://www.example.com', (res) => {
  console.log(`Got response: ${res.statusCode}`);

  res.on('data', (d) => {
    // レスポンスデータを処理する
  });

}).on('error', (e) => {
  console.error(`Got error: ${e.message}`);
});
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developer.eagle.cool/plugin-api/ja-jp/tutorial/network-request.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
