Proxyman iOS
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

1
async function onResponse(context, url, request, response) {
2
// Define JSON Body and Header
3
// Make sure "Content-Type" is "application/json"
4
var param = {
5
body: {
6
"user": {
7
"name": "Proxyman"
8
}
9
},
10
headers: {
11
"Content-Type": "application/json"
12
}
13
}
14
15
// POST request with await
16
var output = await $http.post("https://httpbin.org/post", param);
17
18
// Get Status Code
19
console.log(output.statusCode);
20
21
// Get body
22
console.log(output.body)
23
24
// Get header
25
console.log(output.headers)
26
27
// Done
28
return response;
29
}
Copied!

2. How to use it?

Method

1
var output = await $http.get("https://httpbin.org/anything");
2
var output = await $http.post("https://httpbin.org/anything");
3
var output = await $http.put("https://httpbin.org/anything");
4
var output = await $http.update("https://httpbin.org/anything");
5
var output = await $http.delete("https://httpbin.org/anything");
Copied!

Output format

1
var output = await $http.get("https://httpbin.org/anything");
2
console.log(output)
3
4
// print
5
{
6
"statusCode": <Int>,
7
"headers": <Object>,
8
"body": <Object>
9
}
Copied!

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():
1
async function onRequest(context, url, request) {
2
var output = await $http.get("https://httpbin.org/get");
3
return request;
4
}
5
6
async function onResponse(context, url, request, response) {
7
var output = await $http.get("https://httpbin.org/get");
8
return response;
9
}
Copied!
  • 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
1
var param = {
2
body: {
3
"name": "Proxyman",
4
},
5
headers: {
6
"Content-Type": "application/json"
7
}
8
}
Copied!
Encoded form Body with application/x-www-form-urlencoded
1
var param = {
2
body: {
3
"key1": "value1",
4
"key2": "value2"
5
},
6
headers: {
7
"Content-Type": "application/x-www-form-urlencoded"
8
}
9
}
Copied!