0

The whole idea is to use the LXD RESTful API over a unix socket for local operations. The following code runs well once for each 2 or 3 with errors. I don't know if is related to node.js or is related the lxd api.

The Output with error

problem with request: write EPIPE { [Error: write EPIPE] code: 'EPIPE', errno: 'EPIPE', syscall: 'write', address: undefined }

The code:

/*
Adapted from 
http://rapiddg.com/blog/calling-rest-api-nodejs-script
*/
var querystring = require('querystring');
var http = require('http'), req, response;

var socketUSD='/var/lib/lxd/unix.socket';

function RestConsume(){ };

RestConsume.prototype._doRequest_=function(httpMethod,pathAPI,data,fnCallBack){
        var dataString = JSON.stringify(data);
        var headers = {};

        if (httpMethod==='GET'){
            pathAPI+='?'+querystring.stringify(data);

        }else{
            headers = {
              'Content-Type': 'application/json',
              'Content-Length': dataString.length
            };
        }
        var options = {
                socketPath: socketUSD,
                path: pathAPI,
                method: httpMethod,
                headers: headers
            };

        var req=http.request(options, function (res){
            res.setEncoding('utf-8');

            var resultString="";

            res.on('data',function(data){
                resultString +=data;
            });//end res.on('data')//

            res.on('end',function(){
                console.log(resultString);
                var responseObject = JSON.parse(resultString);
                fnCallBack(resultString);
            });
        });

        req.write(dataString);
        req.end();
        req.on('error', function(e) {
            console.log('Haciendo '+httpMethod);
          console.log('problem with request: ' + e.message);
          console.log(e);
        });
};
RestConsume.prototype.doGet=function(pathAPI,data,fnCallBack){
        return this._doRequest_('GET',pathAPI,data,fnCallBack);
};
RestConsume.prototype.doPost=function(pathAPI,data,fnCallBack){
        this._doRequest_('POST',pathAPI,data,fnCallBack);
};
RestConsume.prototype.doPut=function(pathAPI,data,fnCallBack){
        this._doRequest_('PUT',pathAPI,data,fnCallBack);
};
RestConsume.prototype.doDelete=function(pathAPI,data,fnCallBack){
        return this._doRequest_('DELETE',pathAPI,data,fnCallBack);
};
var obj=new RestConsume();
obj.doGet("/1.0/containers/pc01/logs",{},function(data) {
    console.log('get Respuesta:\r\n' + data);
  });

1 Answer 1

1

Fixed: use the documentation.

More precisely the line that says:

http.request(options, callback).end();

Now, the code works like a charm.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.