Search code examples
httptomcattelnet

Why am I getting a Bad Request response when I try my request with HTTP Protocol?


I have Apache Tomcat running in my local machine currently.

When I try this with telnet:

telnet localhost 8080
Trying ::1...
Connected to localhost.
Escape character is '^]'.
GET /beer/

I will get a response that looks something like:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Beer Expert!</title>
    <meta charset="utf-8">
</head>
<body>

etc, etc..

My first question is: Where is all the HTTP Response Headers such as HTTP/1.1 200 OK etc?

Also when I try my telnet query as follows:

telnet localhost 8080
Trying ::1...
Connected to localhost.
Escape character is '^]'.
GET /beer/ HTTP/1.1

I will get:

HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
Date: Tue, 19 Jan 2016 12:15:23 GMT
Connection: close

0

Connection closed by foreign host.

Why do I see the HTTP Response headers now? And why am I getting a Bad Request response?


Solution

  • With just stating GET /beer/ you're not stating which protocol version you'd like. I'm not sure what tomcat defaults to, but my recommendation is to state the protocol version that you like.

    With HTTP/1.0 you should be in a protocol which is easier to type on telnet. As soon as you go to HTTP/1.0, you'll have more request headers that you can send - e.g.

    GET /beer/ HTTP/1.0
    Host: www.example.com
    

    will reveal that virtual host's content, in case you have many different hosts on one server.

    I'm only using HTTP/1.0 (over 1.1) when I'm telnetting into a webserver - for such a long time that I can't remember if HTTP/1.1 was hard or impossible to type, or if it was just the example that I found when I started doing so. There might be mandatory headers, or information for, e.g. encoding etc, that are missing and make yours a bad request. If HTTP/1.0 is enough for you: run with it. Otherwise let us know what else you need.