How to do Axios request from Firebase Cloud Function

18,223

Solution 1

I changed data.ip to response.data.ip and added return before the two res.status(... lines and the deployed cloud function works for me using Axios when I try it.

The code I have is

    const functions = require('firebase-functions');
    const axios = require('axios');
    const cors = require('cors')({ origin: true });

    exports.checkIP = functions.https.onRequest((req, res) => {
      cors(req, res, () => {
        if (req.method !== "GET") {
          return res.status(401).json({
            message: "Not allowed"
          });
        }
    
        return axios.get('https://api.ipify.org?format=json')
          .then(response => {
            console.log(response.data);
            return res.status(200).json({
              message: response.data.ip
            })
          })
          .catch(err => {
            return res.status(500).json({
              error: err
            })
          })
    
      })
    });

When I invoke the function I get back a reply like { "message": "127.168.121.130" }

Solution 2

I experienced the same issue. An HTTP request with axios returns the following error message : TypeError: Converting circular structure to JSON

Here is an explanation of what is going on and how to get around this

You can use the following package : https://github.com/moll/json-stringify-safe

I'm not sure about the consistency of this approach and personally went for request-promise, which is heavier than axios but allows straightforward HTTP requests.

Share:
18,223
iSaumya
Author by

iSaumya

Updated on July 16, 2022

Comments

  • iSaumya
    iSaumya almost 2 years

    I've tried the following in Firebase Cloud Function to do an Axios request but it didn't work.

        const functions = require('firebase-functions');
        const axios = require('axios');
        const cors = require('cors')({ origin: true });
        
        exports.checkIP = functions.https.onRequest((req, res) => {
            cors(req, res, () => {
                if( req.method !== "GET" ) {
                    return res.status(401).json({
                        message: "Not allowed"
                    });
                }
        
                return axios.get('https://api.ipify.org?format=json')
                .then(data => {
                    console.log(data)
                    res.status(200).json({
                        message: data.ip
                    })
                })
                .catch(err => {
                    res.status(500).json({
                        error: err
                    })
                })
        
            })
        })
    

    I've also googled a lot for seeing some example of how to use Axios with Cloud Functions but found none. The above code is not returning anything.

    Can anyone help?

    P.S.: I've already added billing details in my Firebase account and not using the free Spark plan, rather using Blaze plan.

    Edit:

    I've finally able to do this using the request-promise node package but still no idea about how to do it with axios. As no matter what I try, axios doesn't work in Firebase cloud functions. This is what I did:

    npm i --save cors request request-promise

    Then this is the code I run: https://gist.github.com/isaumya/0081a9318e4f7723e0123f4def744a0e

    Maybe it will help someone. If anyone knows how to do it with Axios please answer below.

  • wzr1337
    wzr1337 over 4 years
    For everyone try to plainly run an example from command line locally: U MUST wrap your function in a functions.https.onRequest().. otherwise it will fail to execute.. took me 30 minutes to figure out how stupid I was think I could just run stuff :D
  • cikatomo
    cikatomo almost 4 years
    what is cors for? can I do without?
  • dcts
    dcts over 3 years
    I tried the example without cors and it still worked perfectly fine.