我的 Twilio 函数无法在 JSON REST API 中获取报价

nomar09

我只想在此链接https://quotes.rest/qod.json 中获取一些特定的引号,但它不起作用。我复制了本教程https://youtu.be/wohjl01HZuY 中的代码,但它对我不起作用。我想从 json 链接获得的报价不适用于下面的函数。任何人都可以帮助我吗?:)

exports.handler = function(context, event, callback) {
    const fs = require('fs');
    fs('https://quotes.rest/qod.json').then(response =>{
        const qotd = JSON.parse(response.body);
        let quote = qotd.contents.quotes[0];
        callback(null,quote);
    });
};
菲尔纳什

Twilio 开发人员布道者在这里。

正如 user3761325 指出的那样,fs是 Node.js 文件系统模块,不会发出 HTTP 请求。有许多库可以轻松地在 Node.js 中发出 HTTP 请求,我个人喜欢got.

创建此视频时,该got模块实际上在 Twilio Functions 中默认可用,但现在已不再如此。您需要安装got到函数依赖项中。您可以在此处查看有关向 Twilio 函数添加依赖的文档

添加got到依赖项后,您可以要求got并使用它来发出视频中的请求:

const got = require("got");

exports.handler = function(context, event, callback) {
    got('https://quotes.rest/qod.json').then(response =>{
        const qotd = JSON.parse(response.body);
        let quote = qotd.contents.quotes[0];
        callback(null, quote);
    });
};

你可以用node-fetchor做同样的事情axios

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我无法从REST API URL获取JSON对象

Twilio REST API如何获取Sid

邮差Twilio Rest Api

REST API 中的 POST - JSON 无法序列化我的数据

Twilio函数调用Studio Rest API-Accountsid错误

如何使用 Python 中的 API 从 Kucoin 获取报价单的价格

无法从 rest API 读取 JSON

我无法在 flutter 中对特定 url 进行 REST api 调用

在twilio调用REST API创建调用中显示不同的号码

我无法使用Kotlin中的Twilio API发送消息,因为错误显示并导致我的应用程序崩溃

无法使用getJSON从Flask Rest API获取Json

从 CoinMarketCap API 以欧元而非美元获取报价 - GAS

从Rest API获取JSON数据

Flutter Rest API获取JSON

无法在烧瓶中为REST API返回json分页

我无法将json发布到我的api lambda函数。我收到{“ message”:“ Missing Authentication Token”}

为什么我的函数无法从JSON文件获取数据?

获取用于调用我们的ASP.NET REST Web API的JSON

由于获取 api 错误,无法从 JSON 文件中获取我的数据

无法从 REST API 调用解析 JSON

无法使用RESTIFY从REST API返回JSON

无法解析JSON REST API响应

在 ng build prod 之后,我无法使用我的 rest api

我无法从 createAsyncThunk 函数中捕获状态和 json 值

我在 Json 文件中的数据未永久保存(Express REST API)

如何在REST API中从Json数组获取JSON对象

无法使用 Guzzle 6.x 从 Twilio 获取 api 数据

如何使用 Twilio 函数中的参数调用外部 API

我无法从我的网站播放twilio录音