我有一个要发布到端点的文本文件(包含geojson)。我可以使用node将文件写出并使用CURL正常发布:
# this works
curl -X POST "https://api/path" \
-F file=@/path/to/my/file.geojson \
--header "Content-Type: multipart/form-data"
发布上述内容没有问题
现在使用节点request
和form-data
模块,API使用相同的确切文件针对同一请求返回500服务器错误:
const data = {
my_file: fs.createReadStream(__dirname + '/file.geojson'),
};
request.post({ url: "https://api/path", formData: data }, (err, resp, body) => console.log('response:', body));
这里的响应始终是500错误...
有指针吗?我假设正在查看request
的文档,即如果您使用form-data
,它应该会自动为您准备多部分请求吗?
https://github.com/request/request#multipartform-data-multipart-form-uploads
CURL在相同的数据,相同的URL,相同的路径下可以正常工作
如果我从form-data
注销fileReadStream的内容,则为
formData { my_file:
ReadStream {
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 65536,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: null,
pipesCount: 0,
flowing: null,
ended: false,
endEmitted: false,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
paused: true,
emitClose: false,
destroyed: false,
defaultEncoding: 'utf8',
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
_events: [Object: null prototype] { end: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
path:
'/path/to/my/file.geojson',
fd: null,
flags: 'r',
mode: 438,
start: undefined,
end: Infinity,
autoClose: true,
pos: undefined,
bytesRead: 0,
closed: false } }