0

I am getting a "socket hang up" error while doing a post request. I am not able to resolve it.

  sparqlQ = getSPARQLPrefix() + query_string;
  console.log(sparqlQ)
  var options = {
    host: process.env['SESAME_HOST'],
    port: process.env['SESAME_PORT'],
    method: 'POST',
    path:
      '/openrdf-sesame/repositories/myReo?update=' +
      encodeURIComponent(sparqlQ) +
      '&content-type=application/sparql-results+json',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept': 'application/sparql-results+json',
    },
  };

  var req = http.request(options, function(res) {
    var data = "";
    res.on('data', function (chunk) {
      data += chunk;
    });
    res.on('error', function (error) {
      console.log(error)
    });
    res.on('end', function () {
       console.log(data)
       req.end();
       callback(null);
    });
  }).on('error', function(e) {
    console.alert("Error getting sesame response [%s]", e.message);
    req.end();
    callback(e.message);
    return
  });

What am I doing wrong? Please help!

GJain
  • 5,025
  • 6
  • 48
  • 82

1 Answers1

1

Two things to mention here.


You are not calling req.end() on your http request.

refer this documentation on the http module of node.js.

With http.request() one must always call req.end() to signify that you're done with the request - even if there is no data being written to the request body.


on the req.error event you are calling console.alert which i think should be console.log


Here is a sample code

http = require("http"); 
var options = {
    host: "localhost",
    port: 80,
    method: 'POST'      
};

var req = http.request(options, function(res) {

    var data = "";
    res.on('data', function (chunk) {
        data += chunk;
    });
    res.on('error', function (error) { });
    res.on('end', function () {
        console.log(data)
        req.end();
        console.log(null);
    });

}).on('error', function(e) {

        console.log("Error getting sesame response [%s]", e.message);
        req.end();
        console.log(e.message);
        return

});

req.end();
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101