简体   繁体   English

如何通过邮寄将图像二进制文件发送到 API? 节点

[英]how can I send an image binary via post to an API? nodejs

I am getting the image binary like this:我得到这样的图像二进制文件:

axios.get("image-url.jpg")

I need to use the response for making a new POST request to another server我需要使用响应向另一台服务器发出新的 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);

Unfortunately that isn't working.不幸的是,这不起作用。 The response I am getting is:我得到的回应是:

data: {
  message: 'This file is not compatible with the picture engine, please try another one',
  error: 'bad_request',
  status: 400,
  cause: []
}

How can I properly do that?我怎样才能正确地做到这一点?

edit: form.getheaders is adding this headeer to the request:编辑: form.getheaders 将此标头添加到请求中:

'content-type': 'multipart/form-data; boundary=--------------------------783115116498484087556627'

all request headers:所有请求标头:

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

all request information:所有请求信息:

_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',

An example of POST Header: POST Header 的示例:

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

as you can see there are important parts like:如您所见,有一些重要的部分,例如:

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

So your script do not read the images, your header is broken as status:400 response from Server says.因此,您的脚本不会读取图像,您的 header 已损坏,因为服务器的状态:400 响应说。

Try this (pseudo code):试试这个(伪代码):

const axios = require('axios');
const fs = require('fs');

var im_config = {
    responseType: 'stream'
};

let im_url =  'image-url' ;

async function getImage() {
    let resp = await axios.get(im_url, im_config);
    //save the image
    resp.data.pipe(fs.createWriteStream('test-image.jpg'));
}

getImage().then(function(result){

  console.log(result); 

  const form = new FormData();
  const url = "server url where send the form ";

  // interesting part 
  form.append('image', 'test-image.jpg');   
  axios.post(url, form, your-config);

});

It worked.有效。 Ok.好的。

To do it on the fly, try to abtain a refer to the downloaded image from 'result' or 'resp' without saving the image.要即时执行此操作,请尝试在不保存图像的情况下从“结果”或“响应”中获取对下载图像的引用。

Otherwise unlink downloaded image after POST is done.否则在 POST 完成后取消链接下载的图像。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过POST正确发送二进制格式的文件? - How do I send a file in binary format properly via POST? 无法通过 POST api 发送图像 - Not able to send image via POST api AngularJS:如何通过$ resource在POST中发送图像文件? - AngularJS: How do I send an image file in POST via $resource? 如何通过使用 fetch API 进行本机反应将图像发布到数据库? - How can I POST an image to DB via react native with the fetch API? 如何通过 post 方法和 nodejs 发送多种编码类型? - How do I send multiple encoding types via a post method and nodejs? 如何在NodeJ中通过POST API推送引用并保存? - How to push reference and save via POST API in NodeJs? 将图像数据发送到API nodejs - Send image data to an API nodejs 如何从Angular HTTP Post向Nodejs后端发送多个参数以在OracleDB查询中使用? - How can I send multiple parameters from Angular HTTP Post to Nodejs backend to use in OracleDB query? 如何将图片发送到 Discord API 请求内容 - How can I send a image into Discord API request content 如何通过post发送json文件,使用ajax到java spring控制器,我正在使用api - How to send a json file via post, using ajax to a java spring controller where i am consuming an api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM