gpt4 book ai didi

javascript - 在 azure 函数中发出 https 请求

转载 作者:行者123 更新时间:2023-11-30 19:19:31 25 4
gpt4 key购买 nike

我正在尝试一个简单的请求:

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 )。

我做错了什么?有没有一种特殊的方法可以在 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()

最佳答案

这是一个工作示例,说明如何请求 Azure AD v2 端点以获取 Azure Function V3( Node 运行时)内的访问 token

var http = require('https');

module.exports = function (context, req) {
var body = "";
body += 'grant_type=' + req.query['grant_type'];
body += '&client_id=' + req.query['client_id'];
body += '&client_secret=' + req.query['client_secret'];
body += '&code=' + req.query['code'];

const options = {
hostname: 'login.microsoftonline.com',
port: 443,
path: '/ZZZZZZZZ-bc69-4c8b-8e91-11f3a181c2bb/oauth2/v2.0/token',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': body.length
}
}

var response = '';
const request = http.request(options, (res) => {
context.log(`statusCode: ${res.statusCode}`)

res.on('data', (d) => {
response += d;
})

res.on('end', (d) => {
context.res = {
body: response
}
context.done();
})
})

request.on('error', (error) => {
context.log.error(error)
context.done();
})

request.write(body);
request.end();
};

这个和 OP 之间的区别:函数没有标记为异步(module.exports = function),我正在使用 context.done(); 让运行时知道异步操作(在我们的例子中是 https 请求)何时完成。 context.done(); 在两个地方:'end' 和 'error' 回调。

如果您想使用异步函数,我认为应该使用 async/await + promises 而不是回调 - link

Using the async and await keywords helps avoid both of these errors. You should use the Node.js utility function util.promisify to turn error-first callback-style functions into awaitable functions.

不是 JS 开发者。

关于javascript - 在 azure 函数中发出 https 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57631020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com