본문으로 건너뛰기

async/await 요청

Proxyman Scripting에서 async/await를 사용해 onRequest와 onResponse 안에서 외부 HTTP/HTTPS API를 호출하세요. macOS에서는 $http를, Windows/Linux에서는 axios를 사용합니다.

1. 기능 소개​

Proxyman macOS v3.5.0 및 Windows/Linux v2.11.0 이상에서는 async / await를 사용해 스크립트 안에서 HTTP/HTTPS 호출을 보내고 외부 리소스를 가져올 수 있습니다.

정보
  • macOS: `$http`를 사용하세요
  • Windows/Linux: 내장 `axios`를 사용하세요

샘플: JSON 본문이 포함된 POST 요청 (macOS)​

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에서 사용하는 방법​

메서드​

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");

출력 형식​

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

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

샘플 코드​

정보

더 많은 샘플 코드는 async/await HTTP 요청 샘플을 확인하세요.

3. Windows/Linux에서 사용하는 방법​

Windows/Linux용 Proxyman에는 내장 axios 라이브러리가 포함되어 있으므로, axios 문법으로 HTTP(S) 요청을 보낼 수 있습니다.

예를 들어 다음과 같습니다.

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

4. 참고 사항​

  • onRequest()와 onResponse()를 async 함수로 정의했는지 확인하세요:
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 프록시를 거치지 않으므로, 다른 디버깅 도구의 영향을 받지 않습니다.
  • onRequest()와 onResponse() 모두에서 await $http.get()을 사용할 수 있습니다.
  • 본문 타입이 Content-Type 헤더와 일치하는지 확인하세요.

application/json 형식의 JSON 본문

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

application/x-www-form-urlencoded 형식의 인코딩된 폼 본문

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