我正在嘗試制作自己的 api,其中一部分是我希望它能夠從不和諧的 webhook 接收文本和檔案,文本部分作業正常,但是當我發送檔案時,我似乎無法弄清楚如何接收它。我環顧了一下,發現它req.files會回傳檔案,但我只是不確定。這是我的api代碼:
const express = require('express'),
bodyParser = require('body-parser'),
base32 = require('base32'),
const pass = "5'g5?cDzy}\\p5zAwvT[DJ/SeD"
const port = 8080
function denied(res) {
res.status(403);
res.send('no');
}
const api = express();
api.use(bodyParser.json());
api.listen(port, () => { console.log(`api running on port ${port}`) });
api.post('/webhook', (req, res) => {
if (!req.headers) {
denied(res);
}
var auth = req.headers['authorization'];
if (auth && base32.decode(auth) === pass) {
console.log(req.files) //undefined
res.send('done');
} else {
denied(res);
}
});
這是我用來發送檔案和文本的代碼:
import requests
key = '6mkped9zcd27mybxbhr3ayj1exv58pu498qn6ta4'
header = {
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
'Authorization': key
}
info = {
"avatar_url":"https://cdn.discordapp.com/attachments/901070985434918965/904813984753000469/nedladdning_14.jpg",
"name":"test",
"embeds": [...]
}
r = requests.post('http://localhost:8080/webhook', headers=header, json=info)
r2 = requests.post('http://localhost:8080/webhook', headers={'Authorization': key}, files={'file': open('test.zip','rb')})
print(r.text, r.status_code)
print(r2.text, r2.status_code)
uj5u.com熱心網友回復:
上傳檔案(例如如何requests)作為multipart/form-data有效負載發送。
根據正文決議器的npm頁面,
[...] 不處理多部分物體,因為它們復雜且通常很大。對于多部分正文,您可能對以下模塊感興趣...
您將需要其中一個模塊,然后您需要閱讀其檔案以查看它是否提供例如。req.files你在某處看到的。否則它確實是未定義的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/430853.html
下一篇:內部訊息佇列與外部訊息佇列
