我的 Twilio 函数无法在内部获取引号 JSON REST API
My Twilio Function Is Not Working In Getting Quotes Inside JSON REST API
我只是想在此 link https://quotes.rest/qod.json 中获取一些特定的引用,但它不起作用。
我从本教程 https://youtu.be/wohjl01HZuY 中复制了代码,但它无法正常工作。我想从 json link 中获取的引用不适用于下面的函数。谁能帮我? :)
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);
});
};
原谅我,但据我所知,FS 模块
代表文件系统模块。
试试这个代码,这是你发送 HTTP 请求的方式。
let handler = function (context, event, callback) {
fetch('https://quotes.rest/qod.json')
.then(response => response.json())
.then(data => {
const gotd = data;
console.log(gotd);
//Your codes Goes Here
});
}
handler()
这里是 Twilio 开发人员布道者。
正如 user3761325 指出的那样,fs
是 Node.js 文件系统模块,不发出 HTTP 请求。 Node.js里面有很多库可以很方便的发起HTTP请求,我个人比较喜欢got
.
创建此视频时,got
模块实际上在 Twilio Functions 中默认可用,但现在已不再如此。您需要将 got
安装到函数依赖项中。您可以查看 the documentation for adding dependencies to your Twilio Function here.
将 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-fetch
or axios
执行相同的操作。
我只是想在此 link https://quotes.rest/qod.json 中获取一些特定的引用,但它不起作用。 我从本教程 https://youtu.be/wohjl01HZuY 中复制了代码,但它无法正常工作。我想从 json link 中获取的引用不适用于下面的函数。谁能帮我? :)
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);
});
};
原谅我,但据我所知,FS 模块 代表文件系统模块。
试试这个代码,这是你发送 HTTP 请求的方式。
let handler = function (context, event, callback) {
fetch('https://quotes.rest/qod.json')
.then(response => response.json())
.then(data => {
const gotd = data;
console.log(gotd);
//Your codes Goes Here
});
}
handler()
这里是 Twilio 开发人员布道者。
正如 user3761325 指出的那样,fs
是 Node.js 文件系统模块,不发出 HTTP 请求。 Node.js里面有很多库可以很方便的发起HTTP请求,我个人比较喜欢got
.
创建此视频时,got
模块实际上在 Twilio Functions 中默认可用,但现在已不再如此。您需要将 got
安装到函数依赖项中。您可以查看 the documentation for adding dependencies to your Twilio Function here.
将 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-fetch
or axios
执行相同的操作。