502 Bad gateway on netlify function

Here is my dev environment: https://greatstate-dev.netlify.app/careers/jobs/test

I’ve written a netlify function which works fine locally using netlify dev, however once this is deployed to dev my post request (aka job form application) returns a 502 bad gateway.

I’ve tried all the things in this support ticket: 502 Bad Gateway error when submitting forms after deployment

but I must be missing something.

Here is the netlify function I’ve written:

const axios = require('axios')

const handler = async function (event, context, callback) {
  // apply our function to the queryStringParameters and assign it to a variable
  let body = {}

  try {
body = JSON.parse(event.body)
const {url,formData} = body;
const options = {
  method: 'POST',
  data: formData,
  headers: {
    Authorization: `Basic ${process.env.API_TOKEN}`,
    'Content-Type': 'application/json',
return axios(options).then(function(response){
  return callback(null, {
    statusCode: response.status,
    body: JSON.stringify({result:response.data}),

}).catch(function(error) {
  let res = {};
  if (error.response) {
     * The request was made and the server responded with a
     * status code that falls out of the range of 2xx
    // console.log(error.response);
    res =  {
      statusCode: error.response.status,
      statusText: error.response.data.error,


  } else if (error.request) {
      * The request was made but no response was received, `error.request`
      * is an instance of XMLHttpRequest in the browser and an instance
      * of http.ClientRequest in Node.js
      res = {
        statusCode: 400,
        statusText: error.request.data.error,

  } else {
    // Something happened in setting up the request and triggered an Error
    // console.log('Error', error.message);
    res = {
      statusCode: 500,
      body: JSON.stringify({result:error.message}),


  return res;

  } catch (e) {



module.exports = { handler }

Any help would be gladly received.

I’ve managed to solve this myself by finding this was actually an issue with gatsby and I have to prefix any client environment variables with “GATSBY” so for example:

1 Like