3

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!!

Kevingo Tsai
  • 629
  • 7
  • 21
  • it works find by using http-request module. http.put({ url: '', reqBody: fs.createReadStream(‘aa.zip'), headers: {'Content-Type':'application/zip'} }, function (err, res) { if (err) { console.error(err); return; } console.log(res.code, res.headers); }); – Kevingo Tsai Apr 10 '15 at 06:56

3 Answers3

7

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.

robertklep
  • 198,204
  • 35
  • 394
  • 381
  • After fs.createReadStream('somefile.zip').pipe(req); How can I make the put request ? – Kevingo Tsai Apr 10 '15 at 01:45
  • var stream = fs.createReadStream('somefile.zip').pipe(req); – Kevingo Tsai Apr 10 '15 at 01:45
  • req.on('error', function(e) { console.log('problem with request: ' + e.message); }); – Kevingo Tsai Apr 10 '15 at 01:46
  • fs.createReadStream('aaa.zip').pipe(request.put(options, function(err, res, data) { if(err) console.log('err: ' + err); else console.log(data); })); But it shows : A header you provided implies functionality that is not implemented .. but I did not set any header in the options, it only contains the uri : var options = { uri:'https://aaa.zzz' }; – Kevingo Tsai Apr 10 '15 at 02:07
  • @KevingoTsai `http.request()` makes the `PUT` request when you pipe the file through it. You seem to be using the [`request`](https://www.npmjs.com/package/request) module, which is fine, but it works (a bit) differently than the plain `http` module that I'm using. As for headers, it _really_ depends on the exact semantics of the API implementation. Perhaps it requires certain headers to be set, I don't know. – robertklep Apr 10 '15 at 06:23
  • @robertkple Finally I use http-request module, which works fine. the code snippet are follows: http.put({ url: '', reqBody: fs.createReadStream(‘aa.zip'), headers: {'Content-Type':'application/zip'} }, function (err, res) { if (err) { console.error(err); return; } console.log(res.code, res.headers); }); – Kevingo Tsai Apr 10 '15 at 06:56
5

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);
    });
liron_hazan
  • 1,396
  • 2
  • 19
  • 27
1

Check that answer.

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

Community
  • 1
  • 1
Alexander Beletsky
  • 19,453
  • 9
  • 63
  • 86