通过要求(“http”)请求将图像作为二进制发送到远程服务器 [英] Sending image as binary via require("http") request to a remote server

查看:431
本文介绍了通过要求(“http”)请求将图像作为二进制发送到远程服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从nodejs服务器向远程服务器发送图像。这是目前为止的请求格式。



注意:就像邮递员中的二进制请求并选择文件并发送一样)

 功能上传(选项,正文){
body = body || ;
返回新的Promise(函数(解析,拒绝){
const https = require('https');
https.request(options,function(response){
var body = [];
response.on('data',function(chunk){
body.push(chunk);
});
response.on('end' ,function(){
resolve(JSON.parse(Buffer.concat(body).toString()))
});
})。on('error',function(error) ){
拒绝(错误);
})。结束(正文);
});
}






使用:

  var options = {
hostname:hostname,
path:/上传,
端口:443,
方法:'PUT',
标题:{
'接受':'application / json',
'Content-Type ':'image / png'
}
};

fs.readFile('./ img / thumbnail.png',函数(错误,数据){
options.body = data;
upload(options).then( ...
});

解决方案

我想你只想要这个:

  fs.createReadStream('。/ img / thumbnail.png')。pipe(https。 request({
hostname:'hostname',
path:'/ upload',
method:'PUT',
headers:{
Accept:'application / json',
'Content-Type':'image / png',
}
},function(response){...}));

您的代码的问题在于您将正文放入选项。正文,但根据文档,它看起来不像比如有任何这样的选择。


I'm trying to send an image to remote server from nodejs server. Here's the request format so far.

Note: Just like binary request in postman and choosing a file and sending)

function upload(options, body) {
    body = body || '';
    return new Promise(function(resolve, reject){
        const https = require('https');
        https.request(options, function(response) {
            var body = [];
            response.on('data', function(chunk) {
                body.push(chunk);
            });
            response.on('end', function(){
                resolve(JSON.parse(Buffer.concat(body).toString()))
            });
        }).on('error', function(error) {
            reject(error);
        }).end(body);
    });
}


Use:

var options = {
    hostname: "hostname",
    path: "/upload",
    port: 443,
    method: 'PUT',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'image/png'
    }
};

fs.readFile('./img/thumbnail.png', function(error, data) {
     options.body = data;
     upload(options).then(...
});

Edit 2

After several attempts, came across an efficient strategy to upload images via streams, here's how it looks like but still not success.

const https = require('https');
var request = https.request(options, function(response) {
    var buffers = [];
    response.on('data', function(chunk) {
        buffers.push(chunk);
    });
    response.on('end', function(){
        console.log(response.headers['content-type']);
        var body = JSON.parse(buffers.length ? Buffer.concat(buffers).toString() : '""');
        response.statusCode >= 200 && response.statusCode < 300 ? resolve(body) : reject(body);
    });
}).on('error', function(error) {
    reject(error);
});

const fs = require('fs');
var readStream = fs.ReadStream(body.path);
readStream.pipe(request);
readStream.on('close', function(){
    request.end();
});

Reference

解决方案

I think you just want this:

fs.createReadStream('./img/thumbnail.png').pipe(https.request({
  hostname: 'hostname',
  path: '/upload',
  method: 'PUT',
  headers: {
    Accept: 'application/json',
    'Content-Type': 'image/png',
  }
}, function (response) { ... }));

The issue with your code is that you were putting the body into options.body, but per the documentation, it doesn't look like there is any such option.

这篇关于通过要求(“http”)请求将图像作为二进制发送到远程服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆