1

I am trying to consume an external api with Firebase Functions, but it gives a time out error when I use OPTIONS, when I use GET to work normally. I don't want to use const request = require('request'); or var rp = require('request-promise');, as they are obsolete. What may be wrong, I await help from colleagues.

const express = require('express');
const cors = require('cors');

const app = express();
// Permitir solicitações de origem cruzada automaticamente
app.use(cors({
    origin: true
}));
//app.use(cors());


app.get('/criarcliente', (req, res) => {

    let uri = "https://api.iugu.com/v1/customers?api_token=<mytoken>";
    let headers = {
        'Content-Type': 'application/json'
    }

    let body = {
        custom_variables: [{
            name: 'fantasia',
            value: 'Dolci Technology'
        }, {
            name: 'vendedor',
        value: ''
        }],
        email: '[email protected]',
        name: 'John Dolci',
        phone: 9999999,
        phone_prefix: 66,
        cpf_cnpj: '00000000000',
        cc_emails: '[email protected]',
        zip_code: '78520000',
        number: '49',
        street: 'Name Street',
        city: 'Guarantã do Norte',
        state: 'MT',
        district: 'Jardim Araguaia'
    }

    var options = {
        method: 'POST',
        uri: uri,
        body: body,
        headers: headers,
        json: true
    };

    const https = require('https');

    var req = https.request(options, (resp) => {
        let data = '';
        resp.on('data', (chunk) => {
            data += chunk;
        });
        resp.on('end', () => {
            var result = JSON.parse(data);
            res.send(result);
        });
        console.log("aqui");
    }).on("error", (err) => {
        console.log("Error: " + err.message);
    });


}); ```

1 Answer 1

1

There are two points that I would point out to your case. First, you need to confirm that you are using the Blaze plan in your billing. As clarified in the official pricing documentation, in case you are not and are trying to use a non Google-owned service, you won't be able to do it and this will cause an error.

The second point would be to use the axios library within your application. It's the one that I believe to be most used with Node.js to allow you access to third-party application within Cloud Functions - I would say that it's pretty easy to use as well. You should find a whole example on using it with third-party API here.

To summarize, take a look at your pricing, as it's the most common issue to be caused and give it a try with axios, to confirm that you can avoid the error with it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer, it has already helped me a lot, me using the Blaze plan. With GET I can normally, I only have problems with PUT and POST, this example that you told me you are using GET, by any chance you don't have an example using POST or PUT?
Hi @LucasDolci you can check here or here for examples using POST. Please, if you think my answer helped you, consider accepting it, so the Community can have a confirmation that this case was solved/helped properly. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.