[英]Consuming external api with Firebase Functions
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.我正在尝试使用 Firebase 函数使用外部 api,但是当我使用 OPTIONS 时它会出现超时错误,当我使用 GET 正常工作时。 I don't want to use const request = require('request');
我不想使用const request = require('request'); or var rp = require('request-promise');
或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: 'teste1@teste1.com',
name: 'John Dolci',
phone: 9999999,
phone_prefix: 66,
cpf_cnpj: '00000000000',
cc_emails: 'test@test.com',
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);
});
}); ```
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.
首先,您需要确认您在账单中使用的是 Blaze 计划。 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.
正如官方定价文档中所阐明的那样,如果您不是并且正在尝试使用非 Google 拥有的服务,您将无法使用,这将导致错误。
The second point would be to use the axios
library within your application.第二点是在您的应用程序中使用
axios
库。 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.我认为它最常与 Node.js 一起使用以允许您访问 Cloud Functions 中的第三方应用程序 - 我想说它也非常易于使用。 You should find a whole example on using it with third-party API here .
您应该在此处找到有关将其与第三方 API 一起使用的完整示例。
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.总而言之,请查看您的定价,因为这是最常见的问题,请尝试使用
axios
,以确认您可以避免错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.