Search code examples
aws-lambdaaxiosform-data

Capture raw axios request from AWS Lambda


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

Solution

  • You might be able to just use a custom request interceptor and interrogate at the requests that way.

    https://axios-http.com/docs/interceptors