简体   繁体   English

使用 axios 从 Firebase 函数访问 NSE 选项链 API

[英]Access NSE option chain API from Firebase Functions using axios

I am trying to get NSE Option Chain json data from firebase functions using axios but request hangs on call.我正在尝试使用 axios 从 firebase 函数获取 NSE 选项链 json 数据,但请求挂起。

NSE API is working fine on firebase-functions local development server,postman,chrome, but fails after deployment, not event giving error msg. NSE API 在firebase-functions 本地开发服务器上运行良好,postman,chrome,但部署后失败,而不是事件给出错误消息。 Logs showing request timeout.显示请求超时的日志。

const functions = require("firebase-functions");
const admin = require('firebase-admin')
const axios = require('axios').default
admin.initializeApp(functions.config().firebase);

exports.getData = functions.region('asia-south1').https.onRequest(async (req, res) => {
    try {
        var resp = await axios({
            url: "https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY",
            headers: {
                'Accept': '*/*',
                'Access-Control-Allow-Origin': '*',
            }
        })
        console.log(resp.data)
        res.json(resp.data)
    } catch (e) {
        console.log(e)
    }
    finally {
        return
    }
})

Google Cloud Functions Log谷歌云函数日志

After suggestion I tried to get error msg like this...建议后我试图得到这样的错误消息......

try {
        var resp = await axios({
            url: process.env.URL,
            headers: {
                'Accept': '*/*',
                'Access-Control-Allow-Origin': '*',
            }
        })
        console.log(resp.data)
        res.json(resp.data)
    } catch (e) {
        functions.logger.error('error block')
        res.json({ e })
    }

getting response "Error: could not handle the request" in chrome在 chrome 中得到响应“错误:无法处理请求”

gcp logs screenshot gcp 日志截图

GCP logs not showing "error block", so its just timeout msg in chrome. GCP 日志未显示“错误块”,因此它只是 chrome 中的超时消息。 Seems NSE API is rejecting request from gcp functions only for some reason.似乎 NSE API 仅出于某种原因拒绝来自 gcp 函数的请求。

replaced NSE API with another api https://jsonplaceholder.typicode.com/todos/1 and function works. replaced NSE API with another api https://jsonplaceholder.typicode.com/todos/1 and function works.

Seems the NSE API needs something specific in header which it gets from chrome and postman but not axios when deployed on google cloud functions. Seems the NSE API needs something specific in header which it gets from chrome and postman but not axios when deployed on google cloud functions.

The API must be returning an error but you are not terminating the function by returning a response. API 必须返回错误,但您不会通过返回响应来终止 function。 Try refactoring the code as:尝试将代码重构为:

exports.getData = functions.region('asia-south1').https.onRequest(async (req, res) => {
  try {
    var resp = await axios({
      ...
    })
    console.log(resp.data)
    res.json(resp.data)
  } catch (e) {
    console.log(e)
    // Return error only for testing
    res.json({
      e
    })
  }
})

This way the function will terminate even if an error is thrown and you should be able to check it.这样,即使抛出错误,function 也会终止,您应该能够检查它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM