7

I want to create a HTTP-1.0 request by net.socket or http.request but I have a problem with this code:

var http = require('http');

var options = {
   host:'google.com',
   method: 'CONNECT',
   path: 'google.com:80',
   port: 80
};

var req = http.request(options);
req.end();

req.on('connect', function(res, socket, head) {
   socket.write('GET / HTTP/1.0\r\n' +
                'Host: google.com:80\r\n' +
                '\r\n');
socket.on('data', function(chunk) {
    console.log(chunk);
});
socket.end();
});

I get same error:

events.js:66
        throw arguments[1]; // Unhandled 'error' event
                   ^
Error: Parse Error
    at Socket.socketOnData (http.js:1366:r20)
    at TCP.onread (net.js:403:27)

Your help is welcome.

Florent
  • 12,310
  • 10
  • 49
  • 58
thename
  • 71
  • 1
  • 2

1 Answers1

5

http.request always uses HTTP/1.1, it's hardcoded in the source.

Here is the code to send HTTP/1.0 request using plain TCP stream (via net.connect):

var net = require('net');

var opts = {
  host: 'google.com',
  port: 80
}

var socket = net.connect(opts, function() {
  socket.end('GET / HTTP/1.0\r\n' +
             'Host: google.com\r\n' +
              '\r\n');
});

socket.on('data', function(chunk) {
  console.log(chunk.toString());
});
Miroslav Bajtoš
  • 10,667
  • 1
  • 41
  • 99