Netlify function post request to external server?

Hello, i want to question, can I use post request inside the netlify function ?

because i’ve been debugging, and i still can’t using post request,

const axios = require('axios')
const FormData = require('form-data')

module.exports.handler = async function(event, context) {
    try {
        const item = {
            "origin": '501',
            "originType": 'city',
            "destination": '496',
            "destinationType": 'subdistrict',
            "weight": 1700,
            "courier": 'jne'
        }

        var formData = new FormData()
        for(let key in item) {
            formData.append(key, item[key]);
        }


        const res = await axios.post(`https://pro.rajaongkir.com/api/cost`, {
            headers: {
                key: process.env.KEY,
                'Access-Control-Allow-Origin': '*',
            },
            // body: JSON.stringify(item)
            body: formData
        })

        return {
            statusCode: res.status,
            body: JSON.stringify(res.data)
        }

    } catch(e) {
        return {
            statusCode: 400,
            body: JSON.stringify(e)
        }

    }
}

I’ve tried to send as formData or as plain object, and nothing works.

do i have to purchase plan or something first like firebase ? (firebase requires me to purchase blaze for posting request to external server) but imnot sure i dont read any here.

also i want to confirm, it seems netlify function cannot process FormData ? i’ve read it somewhere but i dont know.

1 Like

Hi @dellwatson, I’ve see some people report that axios.post doesn’t seem to be working with lambda functions like here: Function seeing axios.post as GET request. That said, could you try fetch and see if you still see the issue?

Hi @dellwatson, did you ever find a resolution to this? Having a similar issue with a post request from a Netlify function.

Thanks.

Hi @Dennis, with fetch I got a similar issue ,
and when I use node-libcurl I get this error

6:27:34 AM: 896f1fbc ERROR Invoke Error {“errorType”:“Error”,“errorMessage”:“Problem with the SSL CA cert (path? access rights?)”,“code”:77,“isCurlError”:true,“stack”:[“Error: Problem with the SSL CA cert (path? access rights?)”]}

@bangaromaric .

Is this question related to the other thread you responded to? If so, let’s consolidate the conversation to one thread. :slight_smile: