简体   繁体   中英

POST Request is working on VS Code but not on AWS Lambda

I was implementing a code to check the tickets of a plate once a day (that's why I want to use Lambda and cron rules).
It works great on VS Code and the output is something like this:

,,AYL729,RNT,G40,VP00263135,25/11/2021,368.00,0.00,246.56,0.00,0.00,121.44,121.44,0.00,Pendiente,0,

,,BHM942,RNT,G40,VP00247677,29/10/2021,368.00,0.00,246.56,0.00,0.00,121.44,121.44,0.00,Pendiente,0,

When I upload the files and libraries to AWS Lambda and test the code, it says that the execution was successful, but the errors catch says "ERROR on POST of plate".

This is the code. What could be wrong? The structure is the following:

  • handler is the POST Request
  • An array of 3 plates
  • A "sleep" function that makes the 500ms to wait from one POST to the next one.
  • A for loop to make all the POST request of the array
import got from "got";
import HttpAgent from "agentkeepalive";
import cheerio from "cheerio";
import CSV from "csv-string";

const { HttpsAgent } = HttpAgent;

export const handler = async (plate) => {
  try {
    const responsePost = await got.post(
      "https://www.sat.gob.pe/VirtualSAT/modulos/papeletas.aspx?mysession=JzULXZg9wGlRWb14hUGYlviexfWY1tZHNDstekr84I%2f8QYR3OSFMaA%3d%3d",
      {
        form: {
          __VIEWSTATE:
            "/wEPDwUKMTg3NTU0MDI4NA9kFgJmD2QWAgIDD2QWBgICDw8WAh4EVGV4dAUISW52aXRhZG9kZAIEDxYCHgdWaXNpYmxlaGQCBQ9kFhBmD2QWAmYPDxYCHgdFbmFibGVkaGRkAgMPZBYCAgEPD2QWAh4Kb25rZXlwcmVzcwUdcmV0dXJuIGRlc2FiaWxpdGFFbnRlcihldmVudClkAgQPZBYCAgEPD2QWAh8DBR1yZXR1cm4gZGVzYWJpbGl0YUVudGVyKGV2ZW50KWQCBg8WAh8BaGQCBw8WBB4Fc3R5bGUFCWhlaWdodDoyOx8BaBYKAgEPPCsADQBkAgMPFgIfAWgWAgIFDxYCHwFoZAIFDxYCHwFoZAIHDxYCHwFoZAIJDw8WAh8BZ2RkAggPDxYCHwFoZGQCCQ8PFgIfAAUKMTgvMDUvMjAyMmRkAgoPDxYCHwFoZGQYAQUiY3RsMDAkY3BsUHJpbmNpcGFsJGdyZEVzdGFkb0N1ZW50YQ9nZOzqpV5JJZcOfU07ozw090P5BJOT",
          __EVENTVALIDATION:
            "/wEWCAKu6a/uBALcjMseAqvUss8LAs7RjMsNAonHvYQEAvfCquoMAvDV84MCApjwi+oOZfjEc15pg1v/tNLE38J32jy8X9Y=",
          ctl00$cplPrincipal$ucDatosCarrito1$valCantidad: 0,
          ctl00$cplPrincipal$txtPlaca: plate,
          ctl00$cplPrincipal$txtPapeleta: "",
          ctl00$cplPrincipal$CaptchaContinue: "Buscar",
          ctl00$cplPrincipal$hidTipConsulta: "busqPlaca",
        },

        agent: {
          https: new HttpsAgent({ rejectUnauthorized: false }),
        },

        headers: {
          "User-Agent":
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36",

          "Content-Type": "application/x-www-form-urlencoded",
        },
      }
    );

    ////////////////////
    //FORMATTING RESPONSE
    ////////////////////
  
  } catch (error) {
    console.log("Error on POST of plate ", plate);
    console.log(error);
  }
};

const plates = ["BHM942", "AYL729"];

const promiseList = plates.map(async function (str) {
  return await handler(str);
});
Promise.all(promiseList);

The error I get is the following:

INFO    HTTPError: Response code 500 (Internal Server Error)
    at Request.<anonymous> (file:///var/task/node_modules/got/dist/source/as-promise/index.js:86:42)
    at Object.onceWrapper (events.js:520:26)
    at Request.emit (events.js:412:35)
    at Request._onResponseBase (file:///var/task/node_modules/got/dist/source/core/index.js:691:22)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async Request._onResponse (file:///var/task/node_modules/got/dist/source/core/index.js:730:13) {
  //Here is a ton of the details of the request}

There are some problems in the code:

  • The use of await outised of an async function.
  • This sleep function will not make the handler executes after 500ms, because the first parameter in the setTimeout is function is going to be executed after the provided amount of time.
  • This is not the best way of handling a loop of promises, what you wanna is use Promise.all()

Since the function handler is already asynchronous, you could do something like this:

const plates = ["BHM942", "AVE847", "ATE776"];

const promiseList = plates.map(async function(str) {
    return await handler(str)
})

Promise.all(promiseList)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM