首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用firebase云功能向非google服务器发送GET请求。

使用firebase云功能向非google服务器发送GET请求。
EN

Stack Overflow用户
提问于 2019-08-19 21:31:52
回答 1查看 679关注 0票数 1

我正在设置Firebase云函数来对非google服务进行API调用。为了进行测试,我使用jsonplaceholder占位符作为API。我收到的答复是一个403错误。

我已经看过其他问题,相似对此,但答案是升级到一个付费的防火墙帐户将解决问题。升级到火势图后,我修复了Firebase阻塞传出的http调用,但是现在我被任何我尝试过的请求返回的403错误所困扰。

代码语言:javascript
运行
复制
import * as functions from 'firebase-functions';
const cors = require('cors')({origin: true});

export const getJson = functions.https.onRequest(async (req, res) => {
    cors( req, res, async ()=> {
        try {
            let info = await req.get('https://jsonplaceholder.typicode.com/todos/1');
            res.send(info);
        } catch (error) {
            res.status(500).send(`catch block hit: ${error}`);
        }
    })
})

我期望API调用返回一个json对象:

代码语言:javascript
运行
复制
{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
}

我收到的回复是:

代码语言:javascript
运行
复制
 "Failed to load resource: the server responded with a status of 403 ()"

注意:如果我从索取请求中生成邮递员,我可以收到预期的结果。

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-19 23:52:34

req是发送到服务器的请求,req.get()用于获取该请求的头部。如果你想提出请求,你需要自己动手。您可以使用原始节点(例如,https包)或引入像axiosisomorphic-fetch这样的包。就像这样:

代码语言:javascript
运行
复制
const functions = require('firebase-functions')
const axios = require('axios')
const cors = require('cors')({ origin: true });

export const getJson = functions.https.onRequest(async (req, res) => {
  cors( req, res, async ()=> {
    try {
      let info = await axios.get('https://jsonplaceholder.typicode.com/todos/1');
      res.send(info.data);
    } catch (error) {
      res.status(500).send(`catch block hit: ${error}`);
    }
  })
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57564187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档