在azure函数内部发出一个https请求 [英] making an https request inside of an azure function
本文介绍了在azure函数内部发出一个https请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试一个简单的请求:
I'm attempting a simple request:
var options = {
host: 'hookb.in',
path: '/8PMoEa9kbaCXgXYxOmdr5',
method: 'POST'
};
var req = http.request(options, (res) => {
var body = context.bindingData.name;
res.on("data", (chunk) => {
body += chunk;
});
res.on("end", () => {
context.res = body;
});
}).on("error", (error1) => {
context.log('error');
context.res = {
status: 500,
body: error1
};
});
req.end();
context.done();
但是,没有响应(目标没有收到任何请求, https://hookbin.com/8PMoEa9kbaCXgXYxOmdr
However, there's no response (and no request received by the target here https://hookbin.com/8PMoEa9kbaCXgXYxOmdr).
我在做什么错?有没有一种特殊的方法可以在azure函数内部创建https请求?
var Jimp = require("jimp");
var http = require('https');
module.exports = async function (context, myBlob) {
context.log("JavaScript blob trigger function processed blob \n Name:", context.bindingData.name, "\n Blob Size:", myBlob.length, "Bytes");
context.log(process.env.ImageConvertedWebHook);
Jimp.read(myBlob, function (err, image) {
image.getBufferAsync(Jimp.MIME_TIFF, function (error, imageData) {
context.log('Node.JS blob trigger function resized ' + context.bindingData.name + ' to ' + image.bitmap.width + 'x' + image.bitmap.height);
context.bindings.outputBlob = imageData;
var options = {
host: 'hookb.in',
path: '/8PMoEa9kbaCXgXYxOmdr5',
method: 'POST'
};
var req = http.request(options, (res) => {
var body = context.bindingData.name;
res.on("data", (chunk) => {
body += chunk;
});
res.on("end", () => {
context.res = body;
});
}).on("error", (error1) => {
context.log('error');
context.res = {
status: 500,
body: error1
};
});
req.end();
context.done();
});
});
};
我也尝试过这种方式:
const data = 'buy milk biotch';
var options = {
host: 'hookb.in',
path: '/8PMoEa9kbaCXgXYxOmdr',
method: 'POST',
port: 443,
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
};
const req = https.request(options, res => {
context.log(`statusCode: ${res.statusCode}`)
res.on('data', d => {
context.log(d)
})
})
req.on('error', error1 => {
context.log(error1)
})
req.write(data)
req.end()
推荐答案
似乎是命名冲突问题.代码中要更改的变量对:
Seems like naming conflict issue .Couple of variables to change in your code:
- 在下面更改
var http = require('https');
收件人
var httpMod = require('https');
- 在下面更改
const req = https.request(options, res => {
context.log(`statusCode: ${res.statusCode}`)
res.on('data', d => {
context.log(d)
})
})
收件人
const customReq = httpMod.request(options, res => {
context.log(`statusCode: ${res.statusCode}`)
res.on('data', d => {
context.log(d)
})
})
希望有帮助.
这篇关于在azure函数内部发出一个https请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文