Capture raw axios request from AWS Lambda

Viewed 660

I have code that calls a vendor API to do a formdata upload of a file by axios from inside an AWS Lambda. The call returns a 400 error. If I run the code locally using the same node version v14 it works. I want to capture both raw requests and compare them for differences. How do I capture both raw requests? I've tried using ngrok and pipedream but they don't show the raw but decode the request and the file.

  let response = null;
  try {
    const newFile = fs.createReadStream(doc);
    const formData = new FormData();
    formData.append("file", newFile);
    formData.append("url", url);
    const headers = {
      Authorization: "Bearer " + token,
      ...formData.getHeaders(),
    };
    console.log("Headers: ", headers);

    response = await axios.post(`${APIBASE}/file/FileUpload`, formData, {
      headers,
    });
    console.log("file upload response", response);
  } catch (err) {
    console.log("fileupload error at API", err);
  }
3 Answers

You're not able to capture the request on the network level, as this is totally controlled by AWS. Maybe there's a way to do this when running in a VPC, but I don't think so.

You could simply use a tool such as axios debug logger to print out all of the request and response contents (including headers etc) before the request is made/after the response has arrived. This might provide some more information as to where things are going wrong.

As to the cause of the problem, it is difficult to help you there since you haven't shared the error message nor do we know anything about the API you're trying to call.

There are multiple ways to debug

  1. axios debug logger .
  2. AWS cloud watch where you can see all the logs. you can capture the request and response.
  3. Use postman to call the prod lambda endpoint and verify the response.
Related