【发布时间】:2020-11-21 20:23:11
【问题描述】:
我已将request-promise 替换为node-fetch。除了一个请求之外,一切都运行良好,它将表单数据发送到一个端点,不幸的是,我无法在此处提供。这是与request-promise 一起使用的代码:
const options = {
method: 'POST',
uri: keys.endpointUrl,
formData: {
operations: JSON.stringify(operations),
map: JSON.stringify(map)
},
json: true
};
const response = await request(options);
移动到node-fetch后,是这样的:
const FormData = require('formdata-node');
const form = new FormData();
form.set('operations', JSON.stringify(operations));
form.set('map', JSON.stringify(map));
const opts = {
method: 'POST',
body: form
};
const res = await fetch(keys.endpointUrl, opts);
console.log(res);
const response = res.json();
res 记录为:
Response {
size: 0,
timeout: 0,
[Symbol(Body internals)]: {
body: PassThrough {
_readableState: [ReadableState],
readable: true,
_events: [Object: null prototype],
_eventsCount: 2,
_maxListeners: undefined,
_writableState: [WritableState],
writable: false,
allowHalfOpen: true,
_transformState: [Object],
[Symbol(kCapture)]: false
},
disturbed: false,
error: null
},
[Symbol(Response internals)]: {
status: 500,
statusText: 'Internal Server Error',
headers: Headers { [Symbol(map)]: [Object: null prototype] }
}
}
我尝试添加不同的标题,但不幸的是,它一直失败。我错过了什么?
NodeJS:v12.16.1 节点获取:2.6.0
【问题讨论】:
标签: node.js request form-data node-fetch