Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a HTTP-1.0 request by net.socket or http.request?

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.

like image 487
thename Avatar asked Sep 14 '12 20:09

thename


People also ask

How do I create a new HTTP request?

The most common HTTP request methods have a call shortcut (such as http. get and http. post), but you can make any type of HTTP request by setting the call field to http. request and specifying the type of request using the method field.

Is HTTP 1.0 still used?

In HTTP 1.0, each TCP connection is responsible for only one object. Even though HTTP 1.1 is the preferred standard for use with mobile applications, and replaced the HTTP 1.0 standard in 1997, there are still a small number of mobile applications using HTTP 1.0.

How is HTTP request built?

An HTTP request is made by a client, to a named host, which is located on a server. The aim of the request is to access a resource on the server. To make the request, the client uses components of a URL (Uniform Resource Locator), which includes the information needed to access the resource.


1 Answers

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());
});
like image 175
Miroslav Bajtoš Avatar answered Oct 01 '22 02:10

Miroslav Bajtoš