0

I Can't set headers in Perl.

print "Expires: Thu, 08 May 2003 08:37:25 GMT\n\n";
print "Content-Type: text/html; charset=windows-1251\n\n";
print "Vary: Accept-Encoding\n\n";

First one works only. Then I have Content-Type: text/x-perl. What is wrong?

3
  • 1
    I forget, does HTTP expect double CRLF between headers, or just one? I know you must include two before the message body. BTW it's usually \r\n for CRLF Commented Dec 16, 2015 at 7:07
  • 2
    If you use modules that do all that for you, you don't need to know these little details. :) Commented Dec 16, 2015 at 8:30
  • 1
    Two new lines are used to separate the headers from the body. So your headers end at your first new line (i.e. after your first header). Commented Dec 16, 2015 at 14:02

1 Answer 1

4

I'll assume you're using CGI to connect your web server to Perl. CGI uses a blank line to separate the headers from the response body. Since

print "Expires: Thu, 08 May 2003 08:37:25 GMT\n\n";

prints a blank line after the Expires: header, the remaining print statements are considered part of the body, not headers. You wanted:

print "Expires: Thu, 08 May 2003 08:37:25 GMT\n";
print "Content-Type: text/html; charset=windows-1251\n";
print "Vary: Accept-Encoding\n\n";
Sign up to request clarification or add additional context in comments.

1 Comment

You might want to remove the second \n for the last header line add another line print "\n"; # finish HTTP header to avoid this exact mistake when adding more header lines in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.