The relevant RFC, Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing contains the answer to your question: that each line of a HTTP request should end with CR/LF.
The grammar for the HTTP Message Format specifies that each header line should end with a Carriage Return character (0x0d
in ASCII) followed by a line feed character (0x0a
):
HTTP-message = start-line
*( header-field CRLF )
CRLF
[ message-body ]
This is expressed more clearly in the description of the Request Line:
A request-line begins with a method token, followed by a single space (SP), the request-target, another single space (SP), the protocol version, and ends with CRLF.
request-line = method SP request-target SP HTTP-version CRLF
Since curl
is a specifically developed for HTTP requests it already uses the appropriate line-endings when making HTTP requests. However, netcat is a more general purpose program. As a Unix utility, it uses line-feed characters for line endings by default, thus requiring the user to ensure that lines are terminated correctly.
You can use the unix2dos
utility to convert the file containing the request headers to use Carriage Return / Line Feed endings.
If you want to type the HTTP request by hand and have a recent version of nc
, you should use its -C
option to use CRLF
for line endings:
nc -C www.youtypeitwepostit.com 80
By the way, it’s worth noting that most popular Internet protocols (e.g., SMTP) use CR/LF line endings.
Note that some web servers (e.g. Apache) are more forgiving and will accept request lines that are terminated only with a Line Feed character. The HTTP specification allows for this, as mentioned in the Message Parsing Robustness section:
Although the line terminator for the start-line and header fields is the sequence CRLF, a recipient MAY recognize a single LF as a line terminator and ignore any preceding CR.
Best Answer
nc
andnetcat
are two names for the same program (typically, one will be a symlink to the other). Though—for plenty of confusion—there are two different implementations of Netcat ("traditional" and "OpenBSD"), and they take different options and have different features.Ncat is the same idea, but from the Nmap project. There is also
socat
, which is a similar idea. There is also/dev/tcp
, an (optional) Bash feature.However, if you're looking to do network speed tests then all of the above are the wrong answer. You're looking for iperf3 (site 1 or site 2 or code).