mko
mko

Reputation: 22044

What's the difference between \r\n and \n

In Terminal it seems like no difference between the two

echo -en 'first\r\nsecond' and echo -en 'first\n\second'

but in the code without \r it doesn't work

echo -en 'GET /test HTTP/1.1\r\nHost: localhost\r\n\r\n' | nc localhost 9292 

works, but

echo -en 'GET /test HTTP/1.1\r\nHost: localhost\n\n' | nc localhost 9292 

doesn't

anyone can explain why it is?

Upvotes: 0

Views: 1812

Answers (1)

tcovo
tcovo

Reputation: 7740

Some applications can handle both \r\n (a.k.a. CRLF, carriage return line feed) and \n (a.k.a. LF, line feed) equivalently as newline sequences. Your terminal is an example.

The HTTP/1.1 Specification dictates that HTTP header lines always end with CRLF. So, an HTTP server which adheres to the specification (such as the one you're running on localhost:9292) will not interpret LF by itself as a valid HTTP header line termination sequence.

Upvotes: 1

Related Questions