在使用JavaScript的Azure函数中,如何调用将请求发送到端点 [英] In azure functions using javascript how to call send request to a endpoint
本文介绍了在使用JavaScript的Azure函数中,如何调用将请求发送到端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Azure函数中,如何使用 javascript 调用API.该请求是带有标头的POST.我尝试使用 XMLHttpRequest ,但出现了类似XMLHttpRequest的异常.
In Azure function how to call an API using javascript. The request is POST with the header.I tried to use XMLHttpRequest, but i got exception like this XMLHttpRequest is not defined.
var client = new XMLHttpRequest();
var authentication = 'Bearer ...'
var url = "http://example.com";
var data = '{.........}';
client.open("POST", url, true);
client.setRequestHeader('Authorization',authentication);
client.setRequestHeader('Content-Type', 'application/json');
client.send(data);
还有其他方法可以实现这一目标,
Any other method is there to achive this,
推荐答案
您可以使用内置的http
模块(node.js的标准模块)来做到这一点:
You can do it with a built-in http
module (standard one for node.js):
var http = require('http');
module.exports= function (context) {
context.log('JavaScript HTTP trigger function processed a request.');
var options = {
host: 'example.com',
port: '80',
path: '/test',
method: 'POST'
};
// Set up the request
var req = http.request(options, (res) => {
var body = "";
res.on("data", (chunk) => {
body += chunk;
});
res.on("end", () => {
context.res = body;
context.done();
});
}).on("error", (error) => {
context.log('error');
context.res = {
status: 500,
body: error
};
context.done();
});
req.end();
};
如果将其安装到Function App中,还可以使用其他任何npm模块,例如request
.
You can also use any other npm module like request
if you install it into your Function App.
这篇关于在使用JavaScript的Azure函数中,如何调用将请求发送到端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文