# async/await リクエスト

> Proxyman Scripting で async/await を使い、onRequest と onResponse から外部の HTTP/HTTPS API を呼び出せます。macOS では $http、Windows/Linux では axios を使います。

## 1. 概要 {#1-whats-it}

Proxyman macOS v3.5.0 以降、および Windows/Linux v2.11.0 以降では、`async / await` を使ってスクリプト内で HTTP/HTTPS の呼び出しを行い、外部のリソースを取得できます。

> **備考:**
>
> * macOS では \`$http\` を使用します
> * Windows/Linux では組み込みの \`axios\` を使用します

### サンプル：JSON ボディを使った POST リクエスト（macOS） {#sample-post-request-with-json-body-macos}

```javascript
async function onResponse(context, url, request, response) {  
  // Define JSON Body and Header
  // Make sure "Content-Type" is "application/json"
  var param = {
    body: {
      "user": {
        "name": "Proxyman"
      }
    },
    headers: {
      "Content-Type": "application/json"
    }
  }

  // POST request with await
  var output = await $http.post("https://httpbin.org/post", param);
  
  // Get Status Code
  console.log(output.statusCode);
  
  // Get body
  console.log(output.body)
  
  // Get header
  console.log(output.headers)
  
  // Done
  return response;
}
```

## 2. macOS での使い方 {#2-how-to-use-on-macos}

### メソッド {#method}

```javascript
var output = await $http.get("https://httpbin.org/anything");
var output = await $http.post("https://httpbin.org/anything");
var output = await $http.put("https://httpbin.org/anything");
var output = await $http.patch("https://httpbin.org/anything");
var output = await $http.delete("https://httpbin.org/anything");
```

### 出力形式 {#output-format}

```javascript
var output = await $http.get("https://httpbin.org/anything");
console.log(output)

// print
{
    "statusCode": <Int>,
    "headers": <Object>,
    "body": <Object>
}
```

### サンプルコード {#sample-code}

* [クエリ付きの GET リクエスト](https://proxyman.com/docs/ja/scripting/snippet-code#get-request-with-query)
* [JSON ボディを使う POST リクエスト](https://proxyman.com/docs/ja/scripting/snippet-code#post-request-with-json-body)
* [application/x-www-form-urlencoded ボディを使う POST リクエスト](https://proxyman.com/docs/ja/scripting/snippet-code#post-request-with-applicationx-www-form-urlencoded-body)
* [PUT / PATCH / DELETE リクエスト](https://proxyman.com/docs/ja/scripting/snippet-code#put--patch--delete-request)

> **備考:** その他のサンプルコードは [async/await による HTTP リクエストのスニペット](https://proxyman.com/docs/ja/scripting/snippet-code#make-asyncawait-http-request-macos) をご覧ください。

## 3. Windows/Linux での使い方 {#3-how-to-use-on-windowslinux}

Proxyman for Windows/Linux には組み込みの [axios](https://github.com/axios/axios) ライブラリが含まれているため、axios の構文で HTTP(S) リクエストを送信できます。

たとえば、次のようになります。

```javascript
async function getUser() {
  try {
    const response = await axios.get('/user?ID=12345');
    console.log(response);
  } catch (error) {
    console.error(error);
  }
}
```

## 4. 注意事項 {#4-notes}

* `onRequest()` と `onResponse()` は、必ず **async** 関数として定義してください。

```javascript
async function onRequest(context, url, request) {
    var output = await $http.get("https://httpbin.org/get");
    return request;
}

async function onResponse(context, url, request, response) {
    var output = await $http.get("https://httpbin.org/get");
    return response;
}
```

* リクエストのタイムアウトは 10 秒です。
* インラインの HTTP リクエストは Proxyman のプロキシを経由しないため、他のデバッグツールの影響を受けません。
* `await $http.get()` は `onRequest()` と `onResponse()` の両方で使用できます。
* ボディの型が Content-Type ヘッダーと一致していることを確認してください。

**application/json の JSON ボディ**

```javascript
var param = {
    body: {
      "name": "Proxyman",
    },
    headers: {
      "Content-Type": "application/json"
    }
  }
```

**application/x-www-form-urlencoded のフォームボディ**

```javascript
var param = {
    body: {
      "key1": "value1",
      "key2": "value2"
    },
    headers: {
      "Content-Type": "application/x-www-form-urlencoded"
    }
  }
```
