Javascript 如何通过post将图像二进制文件发送到API?nodejs

Javascript 如何通过post将图像二进制文件发送到API?nodejs,javascript,node.js,file-upload,multipart,Javascript,Node.js,File Upload,Multipart,我得到的图像二进制如下: axios.get("image-url.jpg") 我需要使用响应向另一台服务器发出新的POST请求 const form = new FormData(); const url = "post-url-here.com"; form.append( "file", new ReadableStream(Buffer.from(file)), "image-te

我得到的图像二进制如下:

axios.get("image-url.jpg")
我需要使用响应向另一台服务器发出新的POST请求

  const form = new FormData();
  const url = "post-url-here.com";
  form.append(
    "file",
    new ReadableStream(Buffer.from(file)),
    "image-test.jpg"
  );
  const config = {
    headers: {
      ...form.getHeaders(),
      Authorization:
        "Bearer token-here",
    },
  };
  axios.post(url, form, config);
不幸的是,这不起作用。我得到的答复是:

data: {
  message: 'This file is not compatible with the picture engine, please try another one',
  error: 'bad_request',
  status: 400,
  cause: []
}
我怎样才能正确地做到这一点

编辑: form.getheaders正在将此标题添加到请求中:

'content-type': 'multipart/form-data; boundary=--------------------------783115116498484087556627'
所有请求头:

headers: {
  Accept: 'application/json, text/plain, */*',
  'Content-Type': 'multipart/form-data; boundary=--------------------------918731250556909112992344',
  Authorization: 'Bearer token-here',
  'User-Agent': 'axios/0.21.1'
},
所有请求信息:

_header: 'POST /pictures/items/upload HTTP/1.1\r\n' +
  'Accept: application/json, text/plain, */*\r\n' +
  'Content-Type: multipart/form-data; boundary=--------------------------116660171215340291632150\r\n' +
  'Authorization: Bearer token-here\r\n' +
  'User-Agent: axios/0.21.1\r\n' +
  'Host: api.mercadolibre.com\r\n' +
  'Connection: close\r\n' +
  'Transfer-Encoding: chunked\r\n' +
  '\r\n',

文章标题示例:

POST /video-upload/ HTTP/1.1
Host: your host 
User-Agent: axios/0.21.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: multipart/form-data; boundary=---------------------------219493771727403213993181042749
Content-Length: 99211703
Connection: keep-alive
如您所见,有以下重要部分:

POST /the-url/ HTTP/1.1
Host: your host 
Content-Length: the length in bytes of your image 
Content-Type: multipart/form-data; boundary=---------------------------219493771727403213993181042749
Connection: keep-alive
所以您的脚本不读取图像,您的标题被破坏为status:400,服务器的响应显示

试试这个(伪代码):

成功了!嗯

若要在运行中执行此操作,请尝试在不保存图像的情况下从“result”或“resp”获取一个参考下载的图像

否则,在POST完成后取消链接下载的图像

fs.unlink('test-image.jpg', (err) => {
  if (err) {
    console.error(err)
    return
  }
}

您是否将图像“image url.jpg”保存在硬盘上,然后选择它将其发布到服务器上,或者需要动态执行此操作?例如:从url获取图像->发布到服务器而不保存图像?Debug form.getHeaders(),或者使用侦听套接字记录整个发布请求,这样我们就可以查看整个post resquest标题,我确实需要立即执行此操作。我正在编辑添加的问题。getHeaders informationcontent类型只是整个标题的一部分。你能发布整个标题吗?是的,这是.getHeaders生成的唯一标题。我再次编辑请求的所有最终标题。我从axios请求中添加了更多信息。但我仍然找不到任何内容长度。这可能是问题所在吗?我们可以尝试下载图像,保存它,然后将其附加到表单并发布到服务器。如果我们在标题中获得更多的数据,我们就有可能在之后动态地执行它。请参阅编辑后的答案。是的,它起作用了。唯一的问题是,我必须保存和删除它们,否则我将有存储问题。你能找到其他方法在不保存任何文件的情况下动态地完成它吗?我认为你也需要销毁缓冲区,除非脚本结束时nodejs帮你完成。但在POST OK之后,响应似乎很简单,即取消链接/删除/删除下载的图像。因此,一个任务完成了,脚本工作了。请在问题标题中添加“Nodejs”
fs.unlink('test-image.jpg', (err) => {
  if (err) {
    console.error(err)
    return
  }
}