Javascript 使用';https';在node.js中
因此,我为我的自动化测试编写了Slack reporter,并希望从不推荐的模块“请求”切换到“https”模块。我更改了发送普通消息的请求,但不知道如何创建发送文件的请求。我在节点文档中找不到任何示例(那里没有“https”的帖子示例),也没有在互联网上找到此类使用的示例。有人能帮我吗 这就是工作要求: 这是我想要的(见下面的编辑)(但不起作用): 我知道node有slackapi,但问题是我需要这个reporter没有任何附加包。我知道request.promise或xhr是可能的,但我需要的是“https” 编辑: 好的,所以我试着去某个地方,我想它应该看起来更像:Javascript 使用';https';在node.js中,javascript,node.js,https,request,slack-api,Javascript,Node.js,Https,Request,Slack Api,因此,我为我的自动化测试编写了Slack reporter,并希望从不推荐的模块“请求”切换到“https”模块。我更改了发送普通消息的请求,但不知道如何创建发送文件的请求。我在节点文档中找不到任何示例(那里没有“https”的帖子示例),也没有在互联网上找到此类使用的示例。有人能帮我吗 这就是工作要求: 这是我想要的(见下面的编辑)(但不起作用): 我知道node有slackapi,但问题是我需要这个reporter没有任何附加包。我知道request.promise或xhr是可能的,但我需要
const file = fs.createReadStream("testLog.txt");
const options = {
channels: "***",
hostname: "slack.com",
port: 443,
path: '/api/files.upload',
method: 'POST',
headers: {
'Authorization': "Bearer ***",
'Content-Type': 'application/json; charset=utf-8',
}
}
const req = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', d => {
process.stdout.write(d)
})
})
req.on('error', error => {
console.error(error)
})
req.write(data)
req.end()
但我不知道如何通过文件来请求写入(数据),因为“数据”必须是字符串、缓冲区、ArrayBuffer、数组或类似数组的对象。
现在,我得到的答复是:
状态代码:200{“确定”:错误,“错误”:“无文件\数据”}%
我也不确定这是否可能,因为SlackAPI说头应该是formData,但这个响应表明这种方法很好。
任何人,请?如果您参考,您可以看到options对象不接受诸如formData
之类的属性
相反,您应该尝试发送类似于中的post数据。如果参考,您可以看到options对象不接受诸如formData
之类的属性
相反,您应该尝试发送post数据,如中所示。我确实在您链接的post中创建了https.request,它正在工作,但这是基于文本的消息。我询问发送的文件是另一种类型,其标题为{“Content type”:“multipart/form data”}。所以它不会起作用。另外,如果消息中通常包含stringify JSON,那么您会在req.write()中添加什么?我将其更改为不包含formData,如下所示:const requestOptions={method:“POST”,url:fileUploadUrl,headers:headers,token:token,title:“Test Log File”,filename:“testLog.txt”,filetype:“auto”,频道:“GHHAZCKS6”,文件:fs.createReadStream(“testLog.txt”)};我得到的是:{“ok”:false,“error”:“unknown_method”,“req_method”:“POST”}我确实创建了一个https.request,就像你链接的帖子一样,它正在工作,但这是一条基于文本的消息。我要求发送一个不同类型的文件,标题为{”内容类型“:“multipart/form data”}。所以它不起作用。另外,如果消息中通常有stringify JSON,您会在req.write()中放入什么?我将其更改为不包含formData,如下所示:const requestOptions={method:“POST”,url:fileUploadUrl,headers:headers,token:token,title:测试日志文件,文件名:“testLog.txt”,文件类型:“auto”,通道:“GHHAZCKS6”,文件:fs.createReadStream(“testLog.txt”)};我得到的是:{“ok”:false,“error”:“unknown_method”,“req_method”:“POST”}
function sendLogFile() {
return new Promise((resolve, reject) => {
const requestOptions = {
url: fileUploadUrl,
headers: headers,
formData: {
token: token,
method: "POST",
title: "Test Log File",
filename: "testLog.txt",
filetype: "auto",
channels: "***",
file: fs.createReadStream("testLog.txt")
}
};
const req = https.request(requestOptions, res => {
res.on("data", d => resolve(d));
});
req.on("error", e => {
reject(e);
});
// Probably that's the part where I'm stuck:
req.write('????????')
req.end();
});
}
const file = fs.createReadStream("testLog.txt");
const options = {
channels: "***",
hostname: "slack.com",
port: 443,
path: '/api/files.upload',
method: 'POST',
headers: {
'Authorization': "Bearer ***",
'Content-Type': 'application/json; charset=utf-8',
}
}
const req = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', d => {
process.stdout.write(d)
})
})
req.on('error', error => {
console.error(error)
})
req.write(data)
req.end()