Links

async/await Request

1. What's it?

From Proxyman 3.5.0, you can use async / await to make an HTTP/HTTPS call for retrieving external resources inside your Script.

Sample: POST Request with JSON Body

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. How to use it?

Method

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.update("https://httpbin.org/anything");
var output = await $http.delete("https://httpbin.org/anything");

Output format

var output = await $http.get("https://httpbin.org/anything");
console.log(output)
// print
{
"statusCode": <Int>,
"headers": <Object>,
"body": <Object>
}

Sample Code

Please checkout the HTTP Snippet code for more sample code.

3. Notes

  • Make sure you defined the async function on onRequest() and onResponse():
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;
}
  • Request Timeout is 10 seconds.
  • The inline HTTP Request doesn't go through the Proxyman Proxy, so it isn't affected by other debugging tools.
  • Use can use await $http.get() on both onRequest() and onResponse()
  • Make sure the Body type is matched with the Content-Type header.
JSON Body with application/json
var param = {
body: {
"name": "Proxyman",
},
headers: {
"Content-Type": "application/json"
}
}
Encoded form Body with application/x-www-form-urlencoded
var param = {
body: {
"key1": "value1",
"key2": "value2"
},
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
}