Home > Mobile >  API gateway returns 502 bad gateway but I can see response cloud watch
API gateway returns 502 bad gateway but I can see response cloud watch

Time:12-15

I am performing a simple api integration which integrated with lamda and api gateway.

const axios = require('axios');
const url = require("url");

exports.handler = async (event, context) => {
  
      // TODO implement
      var maxRJ = event.queryStringParameters.maxResults;
      var jiraLabelJ = event.queryStringParameters.labels;
      var statusJ = event.queryStringParameters.status;
      const config = {
          headers: {
              "Accept": "application/json",
              "Accept-Encoding": "gzip,deflate,compress",
              "Authorization": "XXXXXXXX"
          }
      };
      
      const queryParams = {
          jql: "status=" statusJ " AND labels=" jiraLabelJ "",
          maxResults: maxRJ,
      };

      const params = new url.URLSearchParams(queryParams);
      console.log(params);
    
    const output = axios.get(`apiURL?${params}`, config)
    .then(response => {
          return(JSON.stringify(response.data));
  })
  .catch(error => {console.error(error)
        return(JSON.stringify(error));
  });
  return output;
};

The above lamda is integrated with api gateway with LAMBDA_PROXY . But while testing the api gateway integration I can see the api response but api gatway returns 502

Response Body
{"message": "Internal server error"}

Wed Dec 14 15:29:17 UTC 2022 : Execution failed due to configuration error: Malformed Lambda proxy response
Wed Dec 14 15:29:17 UTC 2022 : Method completed with status: 502
.

Output/test integration shows the log-

Wed Dec 14 15:29:17 UTC 2022 : Endpoint response body before transformations: {api response}

CodePudding user response:

You are returning a response before your promise resolves. You should await your API call and return a proxy response as @AnkushJain commented.

const axios = require('axios');
const url = require("url");

exports.handler = async (event, context) => {
    // TODO implement
    const maxRJ = event.queryStringParameters.maxResults;
    const jiraLabelJ = event.queryStringParameters.labels;
    const statusJ = event.queryStringParameters.status;
    const config = {
        headers: {
            "Accept": "application/json",
            "Accept-Encoding": "gzip,deflate,compress",
            "Authorization": "XXXXXXXX"
        }
    };
      
    const queryParams = {
        jql: "status=" statusJ " AND labels=" jiraLabelJ "",
        maxResults: maxRJ,
    };

    const params = new url.URLSearchParams(queryParams);
    console.log(params);
    try {
        const output = await axios.get(`apiURL?${params}`, config)
        return {
            statusCode: 200,
            body: JSON.stringify(output.data)
        }
    } catch(error) {
        console.error(error)
        return {
            statusCode: 500,
            body: JSON.stringify({
                message: error.message
            })
        }
    }
};

jql: "status=" statusJ " AND labels=" jiraLabelJ "",

This is a security concern. Don't put user provided inputs directly into your JQL statement.

  • Related