async/await Request
Use async/await in Proxyman Scripting to call external HTTP/HTTPS APIs from onRequest and onResponse, with $http on macOS or axios on Windows/Linux.
1. What's it?
From Proxyman macOS v3.5.0 and Windows/Linux v2.11.0 or later, you can use async / await to make an HTTP/HTTPS call and fetch external resources inside your script.
info
- On macOS: Use `$http`
- On Windows/Linux: Use built-in `axios`
Sample: POST Request with JSON Body (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. How to use on macOS?
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.patch("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
- GET Request with Query
- POST Request with JSON Body
- POST Request with application/x-www-form-urlencoded body
- PUT / PATCH / DELETE Request
info
Check out the async/await HTTP request snippets for more sample code.
3. How to use on Windows/Linux?
Proxyman for Windows/Linux ships with a built-in axios library, so you can use the axios syntax to make HTTP(s) requests.
For example:
async function getUser() {
try {
const response = await axios.get('/user?ID=12345');
console.log(response);
} catch (error) {
console.error(error);
}
}
4. Notes
- Make sure you define
onRequest()andonResponse()as async functions:
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;
}
- The request timeout is 10 seconds.
- The inline HTTP request doesn't go through the Proxyman proxy, so other debugging tools don't affect it.
- You can use
await $http.get()in bothonRequest()andonResponse() - Make sure the body type matches 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"
}
}