在上一篇《Nodejs获取Azure Active Directory AccessToken》中,已经获取到了accessToken,现时需要获取WebAPI的数据,选择了node-fetch来获取数据

node-fetch是使用promise的写法,对于习惯了promise写法的人来说,还是非常容易的

这里提醒一下,公司上网是通过代理的方式来上网的,那么在获取外网的地址时,如果没通过代理,则获取不到数据,在这里,我加入了代理node-https-proxy-agent,关于使用,可以看文章最后的参考地址

var fetch = require('node-fetch');
var defaultConfig = require('../config/default.json');
var HttpsProxyAgent = require('https-proxy-agent'); module.exports = {
searchGarmentStyle: function (garmentStyleNo, access_token) {
var queryEntity = {
"filterType": "LEAF",
"filters": [{}],
"attributeName": "item_number",
"searchOperator": "eq",
"filterValue": garmentStyleNo
} var garmentstyles = [];
// Set up the request
return new Promise(function (resolve, reject) {
try {
//------------------------------------------------------------------------------------------------------------- fetch(defaultConfig.apiUrl.getGarmentStyleSigleApi,
{
agent: new HttpsProxyAgent('http://192.168.27.4:8083'),
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer ' + access_token
},
body: JSON.stringify(queryEntity)
})
.then((response) => {
return response.json();
})
.then((responseJsonData) => {
var getresponse = responseJsonData;
if (getresponse.resultType === "SUCCESS") {
if (getresponse.results) {
if (getresponse.results[0].data) {
var getdata = getresponse.results[0].data; for (let i = 0; i < getdata.length; i++) {
garmentstyles.push(getdata[i]);
}
resolve(garmentstyles);
}
else {
reject(new Error('WebAPI Error :resultType.results[0].data Value Is Null Or Empty'));
}
}
else {
reject(new Error('WebAPI Error :resultType.results Value Is Null Or Empty'));
}
}
else {
reject(new Error('WebAPI Error :resultType Value Is ' + getresponse.resultType));
}
})
.catch((error) => {
reject(new Error('WebAPI Error :' + error.message));
}); } catch (e) {
reject(e);
} });
}
};

调用(这里与获取token的方法进行合并):

var garmentstyle_helper = require('./service/garment_style_search');

//get token
adal_manage.getToken()
.then((token_object) => {
// console.log(token_object.accessToken);
//call webapi
garmentstyle_helper
.searchGarmentStyle(garmentStyleNo, token_object.accessToken)
.then((GarmentStyles) => {
if (GarmentStyles && GarmentStyles.length > 0) {
//foreach data
for (var getstyle of GarmentStyles) {
// add message
var message = new builder.Message()
.text(getstyle.linePlanProducts.productID + '(' + getstyle.linePlanProducts.productVersion + getstyle.linePlanProducts.productVersionSerialNo + ')')
.attachmentLayout(builder.AttachmentLayout.carousel)
.attachments(getstyle.linePlanProducts.productMaterialConfigs.map(garmentStyleColorwayAttachment)); session.send(message);
} session.endDialog();
}
else {
// no found
session.send('can not found garment style \"%s\"', garmentStyleNo);
session.endDialog();
}
},
(err) => {
session.send('[searchGarmentStyle Error:]' + err.message ? err.message : '');
session.endDialog();
});
}, (error) => {
session.send('[getToken Error:]' + err.message ? err.message : '');
session.endDialog();
});

参考:https://github.com/bitinn/node-fetch

https://github.com/TooTallNate/node-https-proxy-agent

nodejs使用fetch获取WebAPI的更多相关文章

  1. nodejs 通过 get获取数据修改redis数据

    如下代码是没有报错的正确代码 我通过https获取到数据 想用redis set一个键值存储 现在我掉入了回调陷阱res.on 里面接收到的数据是data 里面如果放入 client.on('conn ...

  2. fetch获取json的正确姿势

    fetch要求参数传递,遇到请求无法正常获取数据,网上其他很多版本类似这样: fetch(url ,{ method: 'POST', headers:{ 'Accept': 'application ...

  3. react native android 上传文件,Nodejs服务端获取上传的文件

    React Native端 使用react-native-image-picker 做出选择图片的操作,选择完成后,直接将图片Post至服务器,保存在服务器的某个地方(保存图片的路径需要公开显示),并 ...

  4. nodejs异常处理过程/获取nodejs异常类型/写一个eggjs异常处理中间件

    前言 今天想写一下eggjs的自定义异常处理中间件,在写的时候遇到了问题,这个错误我捕获不到类型?? 处理过程,不喜欢看过程的朋友请直接看解决方法和总结 看一下是什么: 抛出的异常是检验失败异常Val ...

  5. nodejs微信开发获取token,ticket-1

    /* jshint -W079 */ /* jshint -W020 */ "use strict"; var _ = require("lodash"); v ...

  6. nodejs从服务器获取数据

    // 从服务器获取数据 request('http://192.168.1.7:8080/getDemo', function(error, response, body) { console.log ...

  7. nodejs 不同请求获取前端传的参数

    get方法 参数在req.query中获取 router.get('/', function(req, res, next) { console.log("reqquery:",r ...

  8. 怎么获取WebAPI项目中图片在服务端的路径

    1.这是我的项目结构. 2.路径格式为:[http://服务器域名/文件夹/文件.扩展名] 测试:假如我要获取到[logo_icon.jpg]这张图.在浏览器的地址栏中输入上面那个格式的路径. 3.可 ...

  9. nodejs gulp如何获取参数

    比如执行gulp test 如果需要获取test,使用命令process.argv即可 如果执行gulp test --module aaaa,这句话表示的意思执行gulp test,顺带参数modu ...

随机推荐

  1. poj 2155 Matrix (树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16797   Accepted: 6312 Descripti ...

  2. P1886 滑动窗口

    题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: The array i ...

  3. [NOI2010]能量采集 解题报告

    [NOI2010]能量采集 题目描述 栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量.在这些植物采集能量后,栋栋再使用一个能量汇集机器把这些植物采集到的能量汇集到一起. ...

  4. 工具——SVN常用命令

    SVN一般都是团队合作做一个项目所需用到的,为了是版本的统一 ;1. Check out——从服务器端取得代码    把服务器资料库里存放的某个项目代码取出来,放到本地主机中,这个动作叫做“check ...

  5. [学习笔记]动态dp

    其实就过了模板. 感觉就是带修改的dp [模板]动态dp 给定一棵n个点的树,点带点权. 有m次操作,每次操作给定x,y表示修改点x的权值为y. 你需要在每次操作之后求出这棵树的最大权独立集的权值大小 ...

  6. codeforces902B. Coloring a Tree

    B. Coloring a Tree 题目链接: https://codeforces.com/contest/902/problem/B 题意:给你一颗树,原先是没有颜色的,需要你给树填色成指定的样 ...

  7. checkbox和后面文字无法居中对齐的解决方案

    制作前端页面时,表单的页面中都存在表单元素与提示文字无法对齐的问题.下面是针对这一问题的解决方案: 先上结果图看效果,吼吼~ 最上面两个是经过css处理后的效果,已经居中对齐了哦~,最后一个是没有处理 ...

  8. jquery中的get和post、ajax有关返回值的问题描述

    一:前言 今天我就要离开公司回学校准备考试,在走之前,我自己做的一个模块测试除了一些小的bug.问题如下 我在往数据库中插入数据,首先要选择一级菜单,接着会更具一级菜单生成一级菜单的子目录,在选择日期 ...

  9. WEB-INF 有关的目录路径问题总结

    1.资源文件只能放在WebContent下面,如 CSS,JS,image等.放在WEB-INF下引用不了. 2.页面放在WEB-INF目录下面,这样可以限制访问,提高安全性.如JSP,html 3. ...

  10. (转)C/S 与 B/S 区别

    感谢:http://www.cnblogs.com/xiaoshuai/archive/2010/05/25/1743741.html C/S结构,即Client/Server(客户机/服务器)结构, ...