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!!
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!!
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.
http.request() makes the PUT request when you pipe the file through it. You seem to be using the 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.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);
});
Check that answer.
The only difference would be, you are using .put() instead on .post().