Я пытаюсь позвонить на сервер проверки чеков яблок, используя облачные функции для Firebase. Есть идеи, как сделать HTTP-вызов?
Как сделать HTTP-запрос в Cloud Functions для Firebase?
Ответы (3)
Имейте в виду, что ваш отпечаток зависимости повлияет на время развертывания и холодного запуска. Вот как я использую https.get()
и functions.config()
для проверки связи с другими конечными точками, поддерживаемыми функциями. Вы можете использовать тот же подход при вызове сторонних сервисов.
const functions = require('firebase-functions');
const https = require('https');
const info = functions.config().info;
exports.cronHandler = functions.pubsub.topic('minutely-tick').onPublish((event) => {
return new Promise((resolve, reject) => {
const hostname = info.hostname;
const pathname = info.pathname;
let data = '';
const request = https.get(`https://${hostname}${pathname}`, (res) => {
res.on('data', (d) => {
data += d;
});
res.on('end', resolve);
});
request.on('error', reject);
});
});
person
Dustin
schedule
02.07.2018
Почему
request.on
не находится внутри обратного вызова https.get()
и почему бы не использовать response.on('error')
?
- person Badrush; 25.10.2019
Ответ скопирован из соответствующего редактирования OP
ОП решил это, используя https://github.com/request/request.
var jsonObject = {
'receipt-data': receiptData,
password: functions.config().apple.iappassword
};
var jsonData = JSON.stringify(jsonObject);
var firebaseRef = '/' + fbRefHelper.getUserPaymentInfo(currentUser);
let url = "https://sandbox.itunes.apple.com/verifyReceipt"; //or production
request.post({
headers: {
'content-type': 'application/x-www-form-urlencoded'
},
url: url,
body: jsonData
}, function(error, response, body) {
if (error) {
} else {
var jsonResponse = JSON.parse(body);
if (jsonResponse.status === 0) {
console.log('Recipt Valid!');
} else {
console.log('Recipt Invalid!.');
}
if (jsonResponse.status === 0 && jsonResponse.environment !== 'Sandbox') {
console.log('Response is in Production!');
}
console.log('Done.');
}
});
person
Community
schedule
30.11.2017
в основном с использованием https://nodejs.org/api/https.html
const http = require("http");
const https = require('https');
const mHostname ='www.yourdomain.info';
const mPath = '/path/file.php?mode=markers';
const options = {
hostname: mHostname,
port: 80, // should be 443 if https
path: mPath ,
method: 'GET',
headers: {
'Content-Type': 'application/json'//; charset=utf-8',
}
};
var rData=""
const req0 = http.request(options, (res0)=>
{
res0.setEncoding('utf8');
res0.on('data',(d) =>{
rData+=d;
});
res0.on('end',function(){
console.log("got pack");
res.send("ok");
});
}).on('error', (e) => {
const err= "Got error:"+e.message;
res.send(err);
});
req0.write("body");//to start request
person
kemalony
schedule
20.04.2018
зачем импортировать модуль https, если вы импортировали модуль http?
- person krummens; 09.06.2020
забыл удалить
- person kemalony; 08.08.2020