Invalid status code returned from lambda

stupefied-spence-568c08.netlify.app

I can’t figure out what’s causing this error for this call:
https://stupefied-spence-568c08.netlify.app/.netlify/functions/fetchUrl?url=https://www.dnd5eapi.co/api/spells
“error decoding lambda response: invalid status code returned from lambda: 0”

Code using axios

axios({
    method: "get",
    url: url,
  })
    .then((response) => {
      console.log("data", response.data);
      return {
        statusCode: 200,
        body: JSON.stringify(response.data),
      };
    })
    .catch((error) => {
      console.log(error);
      return {
        statusCode: 500,
        body: JSON.stringify(error),
      };
    });

Hi @rpeltz

Without seeing the rest of your function, I am only making guesses, but it appears you are not returning axios.

This works in my testing

const axios = require('axios');

exports.handler = async (event, context) => {

    const url = event.queryStringParameters.url;
    console.log("URL: ", url)

    return axios({
        method: "get",
        url: url,
      })
        .then((response) => {
          console.log("data", response.data);
          return {
            statusCode: 200,
            body: JSON.stringify(response.data),
          };
        })
        .catch((error) => {
          console.log(error);
          return {
            statusCode: 500,
            body: JSON.stringify(error.message),
          };
        });

}

Or if you don’t want to console.log anything

const axios = require('axios');

exports.handler = async (event, context) => {

    const url = event.queryStringParameters.url;

    return axios({
        method: "get",
        url: url,
      })
        .then((response) => ({
            statusCode: 200,
            body: JSON.stringify(response.data),
        }))
        .catch((error) => ({
            statusCode: 500,
            body: JSON.stringify(error.message),
        }));
}
1 Like