如何将缓冲图像作为 JSON 发送到另一个 API?
How to send buffered image as JSON to another API?
我正在将数据从网页发送到我设计的另一个 API,它将以 JSON 形式接收数据。
这个网页有一个表单,表单数据有 2 个图像和一些文本数据。
我所做的是,我使用 Multer 缓冲了图像,并将原始 req.body.userImage 重新分配给缓冲图像。
另一端的 API 将接收整个 req.body 作为 JSON 并将其保存到 MongoDB 数据库中。由于“userPicture”数据类型是 Buffer,所以我在这里缓冲了使用表单上传的图像,现在我试图将它发送到 API。但是无法发送缓冲图像,因为它是一个 Node.js 项目我使用了 Node.js 的 HTTP 方法,我还使用了 NPM 的其他一些模块,我从它们那里得到了所有同样的错误代码:413(有效载荷太大)。
我现在如何发送缓冲图像?
我是否应该更改另一端 API 的设计以先接收原始图像,然后再在那里缓冲图像?如果是这样,那么我怎样才能将原始图像发送到 API?
const upload = multer({
limits: {
fileSize: 2000000,
},
fileFilter(req, file, cb) {
if (!file.originalname.match(/\.(jpg|png|jpeg|svg)$/)) {
return cb(new Error("Please upload an image"));
}
cb(undefined, true); //means go ahead and accept the given upload
},
});
app.post("/postData", upload.any('photos'), async (req, res) => {
console.log('data send here')
// console.log(req.body)
// console.log(req.body.name)
// console.log(req.files[0].buffer)
let distroLogo = await sharp(req.files[0].buffer)
.resize({
width: 300,
height: 300
})
.png()
.toBuffer();
req.body.logo = distroLogo
let userPicture = await sharp(req.files[1].buffer)
.resize({
width: 300,
height: 300
})
.png()
.toBuffer();
req.body.userProfilePicture = userPicture
const data = JSON.stringify(req.body)
const options = {
hostname: '***API ADDRESS***',
port: 443,
path: '/**PATH**',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
}
const reque = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`)
reque.on('data', (d) => {
process.stdout.write(d)
})
})
reque.on('error', (error) => {
console.error(error)
})
reque.write(data)
reque.end()
}), (error, req, res, next) => {
res.status(400).send({
error: error.message
})
}
您可以使用“Transfer-Encoding: chunked” header
如果您的请求尚未使用压缩,您可以使用:gzip、br 或 deflate。
我通过使用 bodyParser 限制和扩展选项解决了这个问题,这意味着我使用以下代码来增加 bodyParser 的限制以获取超过默认值的值。
添加代码:
app.use(bodyParser.json({
limit: "50mb"
}));
app.use(bodyParser.urlencoded({
limit: "50mb",
extended: true,
parameterLimit: 50000
}));
我使用了以下 post 来解决我的问题:
我正在将数据从网页发送到我设计的另一个 API,它将以 JSON 形式接收数据。 这个网页有一个表单,表单数据有 2 个图像和一些文本数据。 我所做的是,我使用 Multer 缓冲了图像,并将原始 req.body.userImage 重新分配给缓冲图像。 另一端的 API 将接收整个 req.body 作为 JSON 并将其保存到 MongoDB 数据库中。由于“userPicture”数据类型是 Buffer,所以我在这里缓冲了使用表单上传的图像,现在我试图将它发送到 API。但是无法发送缓冲图像,因为它是一个 Node.js 项目我使用了 Node.js 的 HTTP 方法,我还使用了 NPM 的其他一些模块,我从它们那里得到了所有同样的错误代码:413(有效载荷太大)。 我现在如何发送缓冲图像? 我是否应该更改另一端 API 的设计以先接收原始图像,然后再在那里缓冲图像?如果是这样,那么我怎样才能将原始图像发送到 API?
const upload = multer({
limits: {
fileSize: 2000000,
},
fileFilter(req, file, cb) {
if (!file.originalname.match(/\.(jpg|png|jpeg|svg)$/)) {
return cb(new Error("Please upload an image"));
}
cb(undefined, true); //means go ahead and accept the given upload
},
});
app.post("/postData", upload.any('photos'), async (req, res) => {
console.log('data send here')
// console.log(req.body)
// console.log(req.body.name)
// console.log(req.files[0].buffer)
let distroLogo = await sharp(req.files[0].buffer)
.resize({
width: 300,
height: 300
})
.png()
.toBuffer();
req.body.logo = distroLogo
let userPicture = await sharp(req.files[1].buffer)
.resize({
width: 300,
height: 300
})
.png()
.toBuffer();
req.body.userProfilePicture = userPicture
const data = JSON.stringify(req.body)
const options = {
hostname: '***API ADDRESS***',
port: 443,
path: '/**PATH**',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
}
const reque = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`)
reque.on('data', (d) => {
process.stdout.write(d)
})
})
reque.on('error', (error) => {
console.error(error)
})
reque.write(data)
reque.end()
}), (error, req, res, next) => {
res.status(400).send({
error: error.message
})
}
您可以使用“Transfer-Encoding: chunked” header 如果您的请求尚未使用压缩,您可以使用:gzip、br 或 deflate。
我通过使用 bodyParser 限制和扩展选项解决了这个问题,这意味着我使用以下代码来增加 bodyParser 的限制以获取超过默认值的值。
添加代码:
app.use(bodyParser.json({
limit: "50mb"
}));
app.use(bodyParser.urlencoded({
limit: "50mb",
extended: true,
parameterLimit: 50000
}));
我使用了以下 post 来解决我的问题: