Hi everyone!
I want to sent http post requests via nodejs code to a logstash with a basic configuration file :
input { http { } }
output {
elasticsearch { hosts => ["localhost:9200"] }
stdout { codec => rubydebug }
}
and here is my nodejs script
var querystring = require('querystring');
var http = require ('http');
var options = {
host: "192.168.69.112",
port: 8080,
path: "/my-path/my-device/1",
method: "POST",
headers: {
"Content-type": "application/x-www-form-urlencoded;charset=UTF-8'",
"Accept": "text/plain",
}
};
var postData = querystring.stringify({
"message":"test"
});
var post_option = http.request(options, function(res){
res.setEncoding("utf8");
res.on("data", function(chunk){
console.log("BODY: ${chunk}");
});
res.on('end', function(finish) {
console.log('No more data in response.');
console.log(postData.toString());
});
});
post_option.write(postData);
post_option.end();
Each time I send a request, It is recognized get an error on logstash :
2017-03-20 14:33:47 +0100: HTTP parse error, malformed request (): #<Puma::HttpParserError: Invalid HTTP format, parsing fails.>
2017-03-20 14:33:47 +0100: ENV: {"rack.version"=>[1, 3], "rack.errors"=>#<IO:fd 2>, "rack.multithread"=>true, "rack.multiprocess"=>false, "rack.run_once"=>false, "SCRIPT_NAME"=>"", "QUERY_STRING"=>"", "SERVER_PROTOCOL"=>"HTTP/1.1", "SERVER_SOFTWARE"=>"2.16.0", "GATEWAY_INTERFACE"=>"CGI/1.2"}
---
{
"headers" => {
"http_accept" => "text/plain",
"content_type" => "application/x-www-form-urlencoded;charset=UTF-8'",
"request_path" => "/my-path/my-device/1",
"http_version" => "HTTP/1.1",
"http_connection" => "keep-alive",
"http_transfer_encoding" => "chunked",
"request_method" => "POST",
"http_host" => "192.168.69.112:8080",
"request_uri" => "/my-path/my-device/1"
},
"@timestamp" => 2017-03-20T13:33:47.467Z,
"@version" => "1",
"host" => "192.168.69.241",
"message" => ""
}
plus the message field is empty. I don't understand since my code is able to send post requests to a server.
I've tried this example :
And the curl commands are working, so I guess the problem comes from the nodejs script but I can't find it... Can you help me?
Thanks