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.
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.
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.
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.
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());
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With