2

I have a Thin server running locally, serving a Rails app. Following an example in the netcat man page, I'm trying to use nc to talk to my server:

echo -n "GET / HTTP/1.1\r\n\r\n" | nc 0.0.0.0 3000

But I get a 400 response:

HTTP/1.1 400 Bad Request
Content-Type: text/plain
Connection: close
Server: thin 1.6.1 codename Death Proof

What am I missing?

ivan
  • 4,968
  • 6
  • 38
  • 56

1 Answers1

2

HTTP 1.1 requires providing a Host: header. You also need to add the -e flag to the echo command to escape character sequences, so

echo -en "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n" | nc 0.0.0.0 3000

would work, or alternatively you could use HTTP 1.0 which does not require the Host: header, so

echo -en "GET / HTTP/1.0\r\n\r\n" | nc 0.0.0.0 3000
Hans Z.
  • 41,402
  • 9
  • 80
  • 105
  • You're right on both counts. Thanks! Weird that the man page for `echo` makes no mention of the `-e` flag, at least on my system. – ivan Jan 10 '15 at 14:46
  • that's because you're using the shell builtin command, but the man page is for the `/bin/echo` command – Hans Z. Jan 10 '15 at 14:50