我有一个采用二进制文件流的API。我可以使用邮递员访问API。
现在在服务器端,XML的内容位于字符串对象中,因此我首先创建了流,然后使用axios lib(以调用第三方API)将其与表单数据一起发布。这就是我的做法
const Readable = require("stream").Readable;
const stream = new Readable();
stream.push(myXmlContent);
stream.push(null); // the end of the stream
const formData = new FormData();
formData.append("file",stream);
const response = await axios({
method: "post",url: `${this.BASE_URL}/myurl`,data: formData
});
return response.data;
但这不能正确发送数据,因为第三方API抛出Bad Request: 400
。
如何将XML字符串内容作为流发送到API?