0

I am building a Firebase cloud function that connects to merchant's website and requests a PaymentIntent object.

I'm submitting the API call via axios:

exports.getPaymentIntent = functions.https.onCall(async (data, context) => {
    // Initialise dependencies
    const axios = require('axios')
    const FormData = require('form-data')

    // Assemble payload
    let formData = new FormData()
    formData.append('amount', '10.99')
    ...

    try {
        const response = await axios.post('merchant.com/getPaymentIntent', formData, {
            headers: {
                'X-API-KEY': 'myPrivateKey'
            }
        })

        return response
    } catch (error) {
        return error
    }
})

When I run the function, it crashes with the following axios error:

Unhandled error RangeError: Maximum call stack size exceeded

I can't find out why this is happening. Removing the headers from the request doesn't change the error message either.

Is there anything I can try to fix this please?

Sam
  • 1,130
  • 12
  • 36

2 Answers2

0

I found the solution few answers deep here.

I had to add an additional header in the request (on top of the X-API-KEY header):

'Content-Type': multipart/form-data; boundary=${formData._boundary}.

Setting it as just multipart/form-data would not work, so make sure you include the boundary.

Sam
  • 1,130
  • 12
  • 36
0

I was having same issue, and in my case I fixed by returning response.data instead of just response. Seems that the response object is huge and Firebase breaks when try to parse it. You can confirm that by running console.log(response).

Marcio J
  • 673
  • 7
  • 17