我使用request-promise
npm lib从NodeJS app到Java Jersey 2.0 API后端制作多部分请求。由于request
使用form-data
lib进行调用,我发现它只能发送字符串,缓冲区和流。
在java后端方面,我期待POJO对象为@FormDataParam
s:
@GET
@ApiOperation(
value = "Gets party metadata by party_id",
notes = "Gets party metadata by party_id"
)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "metadata")
})
@Path("party")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Metadata getByPartyId(
@ApiParam(value = "partyId", required = true) @NotNull @QueryParam("partyId") String partyId,
@ApiParam(value = "party metadata", required = true) @FormDataParam("metadata") Metadata metadata
) {
return folderCtrl.getByPartyId(partyId);
}
有一些端点,我甚至等待多个POJO对象来到,我正在使用注释验证机制在接收到端点时验证它们。 到目前为止,我能够模拟所需的行为,因为我将这些POJO作为包含JSON数据的文件发送。泽西岛/杰克逊能够将其转变为POJO并继续其生命周期......
现在,我需要从NodeJS调用这些端点。
因此,我知道可以使用form-data
发送什么,我想我需要一个选项,将JSON对象转换为流,并像以前的场景一样发送它以接收POJO对象。
所以我的问题是,如何在NodeJS中“简化”对象并使用request-promise
发送它们?
这就是我现在所拥有的:
'use strict';
var rp = require('request-promise');
var express = require('express');
var fs = require('fs');
var metaJson = require(__dirname + '/attachments/metadata.json');
var router = express.Router();
const Readable = require('readable-stream').Readable;
const stream = Readable({objectMode: true});
stream._read = () => {};
const PROTOCOL = 'http://';
const HOST = 'localhost:';
const PORT = 8080;
// define the home page route
router.get('/', async function (req, res) {
stream.push(metaJson);
const options = {
method: req.method,
json: true,
baseUrl: PROTOCOL + HOST + PORT,
uri: req.originalUrl,
qs: {
partyId: "qwer"
},
formData: {
metadata: stream
},
};
try {
const response = await rp(options);
} catch (err) {
console.log(err);
}
res.send('foo res');
});
但最终出现了这个错误:
_http_outgoing.js:502
throw new TypeError('First argument must be a string or Buffer');
^
TypeError: First argument must be a string or Buffer
at ClientRequest.write (_http_outgoing.js:502:11)
at Request.write (/home/tepo/IdeaProjects/rig/node_modules/request/request.js:1514:27)
at FormData.ondata (internal/streams/legacy.js:16:26)
at emitOne (events.js:96:13)
at FormData.emit (events.js:191:7)
at FormData.CombinedStream.write (/home/tepo/IdeaProjects/rig/node_modules/combined-stream/lib/combined_stream.js:118:8)
at DelayedStream.ondata (internal/streams/legacy.js:16:26)
at emitOne (events.js:96:13)
at DelayedStream.emit (events.js:191:7)
at DelayedStream._handleEmit (/home/tepo/IdeaProjects/rig/node_modules/delayed-stream/lib/delayed_stream.js:82:15)
at Readable.source.emit (/home/tepo/IdeaProjects/rig/node_modules/delayed-stream/lib/delayed_stream.js:29:19)
at Readable.read (/home/tepo/IdeaProjects/rig/node_modules/readable-stream/lib/_stream_readable.js:386:26)
at flow (/home/tepo/IdeaProjects/rig/node_modules/readable-stream/lib/_stream_readable.js:734:34)
at resume_ (/home/tepo/IdeaProjects/rig/node_modules/readable-stream/lib/_stream_readable.js:717:3)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
答案 0 :(得分:0)
当您使用Express来处理NodeJs中的文件时别忘了使用“Multer”模块(已从Express中删除),该模块用于处理快递文件