How to make http put request with zip file in nodejs

大城市里の小女人 提交于 2019-12-07 15:29:07

问题


I want to make a http PUT request with zip file in binary to a web api and get response with http status code.

How to read the file and PUT it with binary ?

Thank you for your help!!


回答1:


You can start with this:

var http = require('http');
var fs   = require('fs');

var req = http.request({
  hostname : HOSTNAME,
  port     : PORT,
  path     : UPLOAD_PATH,
  method   : 'PUT',
});

fs.createReadStream('somefile.zip').pipe(req);

You may need to perform some other actions, like proper error handling, setting Content-Type headers, etc.




回答2:


Using request-promise (based on bluebird)

const fs = require('fs');
const request = require('request-promise');

const options = {
    method: 'PUT',
    url: 'dest url',
    qs: {key: 'value'}, // optional 
    headers: {
        'content-type': 'application/octet-stream'
    }
};

fs.createReadStream(zipFilePath).pipe(request(options)).then(body =>{
        console.log(body);
    })
    .catch(err => {
        console.log(err);
    });



回答3:


Check that answer.

The only difference would be, you are using .put() instead on .post().



来源:https://stackoverflow.com/questions/29536461/how-to-make-http-put-request-with-zip-file-in-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!