I've been developing a web client to interact with a REST API server, and would like to use PATCH method.
Although I've tried to write a request body into PATCH's request, I found the body remains empty. PUT or POST works fine in the same way though.
I can use PUT instead, but does anyone know if my usage of http module is wrong?
Thank you in advance.
var http = require('http');
module.exports = {
patch: function(path, data, done, fail){
var jsonData = JSON.stringify(data);
var options = {
headers: {
'Content-Type':'application/json;charset=UTF-8',
'Content-Length':jsonData.length,
}
};
var req = this.request(path, "PATCH", done, fail, options);
// THIS CODE DOESN'T WRITE jsonData INTO REQUEST BODY
req.write(jsonData);
req.end();
},
request: function(path, method, done = () => {}, fail = () => {}, options = { headers: {} } ){
options.path = path;
options.method = method;
return http.request(options, function(res){
var body = '';
res.setEncoding('utf8');
res.on("data", function(chunk){
body += chunk;
});
res.on("end", function(){
// process after receiving data from server
});
}).on("error", function(e) {
// process after receiving error
});
}
}