Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What, at the bare minimum, is required for an HTTP request?

Tags:

http

I'm trying to issue a GET command to my local server using netcat by doing the following:

echo -e "GET / HTTP/1.1\nHost: localhost" | nc localhost 80 

Unfortunately, I get a HTTP/1.1 400 Bad Request response for this. What, at the very minimum, is required for a HTTP request?

like image 976
Naftuli Kay Avatar asked Jul 13 '11 22:07

Naftuli Kay


People also ask

What is the only required aspect of an HTTP request?

Content-Length or Transfer-Encoding are only mandatory if an entity is delivered with the request or response, and in many cases a request or response will lack an entity (like a GET request, or a 302 response).

What is required in an HTTP response?

The first line of the response is mandatory and consists of the protocol ( HTTP/1.1),response code (200)and description (OK). The headers shown are: CONTENT-Type -This is Text/html which is a web page. It also includes the character set which is UTF-8.

What is the average size of HTTP request?

Request headers today vary in size from ~200 bytes to over 2KB. As applications use more cookies and user agents expand features, typical header sizes of 700-800 bytes is common.


2 Answers

if the request is: "GET / HTTP/1.0\r\n\r\n" then the response contains header as well as body, and the connection closes after the response.

if the request is:"GET / HTTP/1.1\r\nHost: host:port\r\nConnection: close\r\n\r\n" then the response contains header as well as body, and the connection closes after the response.

if the request is:"GET / HTTP/1.1\r\nHost: host:port\r\n\r\n" then the response contains header as well as body, and the connection will not close even after the response.

if your request is: "GET /\r\n\r\n" then the response contains no header and only body, and the connection closes after the response.

if your request is: "HEAD / HTTP/1.0\r\n\r\n" then the response contains only header and no body, and the connection closes after the response.

if the request is: "HEAD / HTTP/1.1\r\nHost: host:port\r\nConnection: close\r\n\r\n" then the response contains only header and no body, and the connection closes after the response.

if the request is: "HEAD / HTTP/1.1\r\nHost: host:port\r\n\r\n" then the response contains only header and no body, and the connection will not close after the response.

like image 200
Abhishek Oza Avatar answered Sep 19 '22 13:09

Abhishek Oza


It must use CRLF line endings, and it must end in \r\n\r\n, i.e. a blank line. This is what I use:

printf 'GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: close\r\n\r\n' |   nc www.example.com 80 

Additionally, I prefer printf over echo, and I add an extra header to have the server close the connection, but those aren’t needed.

like image 23
Josh Lee Avatar answered Sep 22 '22 13:09

Josh Lee