Saltar al contenido principal

Solicitudes con async/await

Usa async/await en Proxyman Scripting para llamar a APIs HTTP/HTTPS externas desde onRequest y onResponse, con $http en macOS o axios en Windows/Linux.

1. ¿Qué es?​

Desde Proxyman macOS v3.5.0 y Windows/Linux v2.11.0 o posterior, puedes usar async / await para hacer llamadas HTTP/HTTPS y obtener recursos externos dentro de tu script.

información
  • En macOS: usa `$http`
  • En Windows/Linux: usa el `axios` integrado

Ejemplo: solicitud POST con cuerpo JSON (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. ¿Cómo usar en macOS?​

Métodos​

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

Formato de salida​

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

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

Código de ejemplo​

información

Consulta los ejemplos de solicitudes HTTP async/await si quieres ver más código de ejemplo.

3. ¿Cómo usar en Windows/Linux?​

Proxyman para Windows/Linux incluye de serie la librería axios, así que puedes usar la sintaxis de axios para hacer solicitudes HTTP(S).

Por ejemplo:

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

4. Notas​

  • Asegúrate de definir onRequest() y onResponse() como funciones 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;
}
  • El tiempo de espera de la solicitud es de 10 segundos.
  • La solicitud HTTP incluida en el script no pasa por el proxy de Proxyman, así que otras herramientas de depuración no la afectan.
  • Puedes usar await $http.get() tanto en onRequest() como en onResponse()
  • Asegúrate de que el tipo de cuerpo coincida con el encabezado Content-Type.

Cuerpo JSON con application/json

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

Cuerpo de formulario codificado con application/x-www-form-urlencoded

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