无法将图像作为表单有效负载元素发布到 Node.js API

问题描述 投票:0回答:1

我在 Node.js 下的 Javascript 方面遇到了很大的困难,它应该从磁盘读取文件并通过后期请求将其发布到 DeepStack 面部识别系统以对其执行某些操作。

我目前总是收到 400 代码 -->“未找到有效的图像文件”

我认为这可能与文件没有通过 Axios 作为后有效负载传输有关。

我正在使用以下代码:

const fs = require("fs")
const axios = require("axios")
const FormData= require('form-data')

var image_stream = fs.createReadStream('./somefile.jpg')
var form = new FormData()
form.append('image', image_stream);
axios({
    method: "post",
    url: "http://192.168.178.46:666/v1/vision/face/recognize",
    headers: { 'Content-Type': 'multipart/form-data'},
    data: form,
    timeout: 1000
}).then(function (response) {
   // do some stuff with the response
}).catch(function(error){log(error)})

createReadStream
ReadFileSync
都不起作用,而且我在标题和表单数据上尝试过的所有内容都不起作用。 我也尝试过
fetch
和其他包来发送数据,但没有成功。

你能帮我提供建议吗?

node.js axios
1个回答
0
投票

多部分请求可能没有很好地形成,所以,尝试:

  • 省略
    content-type
    标题:
axios({
    method: "post",
    url: "http://192.168.178.46:666/v1/vision/face/recognize",
    data: form,
    timeout: 1000
  • 或者使用FormData的方法来设置标题:
axios({
    method: "post",
    url: "http://192.168.178.46:666/v1/vision/face/recognize",
    headers: { ...form.getHeaders() },
    data: form,
    timeout: 1000 
© www.soinside.com 2019 - 2024. All rights reserved.