Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Print to Browser Headers Not Working

by Loops303 (Novice)
on Feb 27, 2013 at 15:28 UTC ( #1020908=perlquestion: print w/replies, xml ) Need Help??
Loops303 has asked for the wisdom of the Perl Monks concerning the following question:

Hello wise monks, I seek your guidance and wisdom to overcome a wall that I have hit. I am running Perl v.5.12.4 on Mac OS X. My problem is I am creating a script that is accessed via a browser (Safari) that will create and download a CSV file. When I run the script locally, the content of the file is being displayed on the page, and not being downloaded. I have searched high and low, and used the correct headers (as far as I can tell). To troubleshoot, I wanted to see if the browser is accepting any headers, and therefore tested this script (contains no download, just output HTML to the browser):

#!/usr/bin/perl print "Content-type: text/html\n\n"; print "<html><head>"; print "<title>CGI Test</title>"; print "</head>"; print "<body><h2>I just wrote a web page using Perl!</h2>"; print "</body></html>"

The result -- the following is printed on the browser page:

Content-type: text/html <html><head><title>CGI Test</title></head><body><h2>I just wrote a web + page using Perl!</h2></body></html>

It seems that no matter what headers I try, the result is the same -- it is printed to the web page. I cannot for the life of me figure out how to solve this, and therefore graciously request your wisdom. Thank you.

Replies are listed 'Best First'.
Re: Print to Browser Headers Not Working
by Corion (Pope) on Feb 27, 2013 at 15:31 UTC

    To tell the browser you want it to save the content as a file, you need the Content-Disposition header:

    Content-Disposition: attachment; filename="example.csv"
Re: Print to Browser Headers Not Working
by aitap (Curate) on Feb 27, 2013 at 15:56 UTC
    What HTTP server do you run with this Perl script? This looks like a server setup problem. (more info: Common Gateway Interface, CGI)
    Sorry if my advice was wrong.

      In particular I suspect the OP isn't accessing the script through an HTTP server at all, but perhaps saving the script output to a file and opening it.

      package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

      I'm a newb so please forgive me. I'm running the apache server that comes with OS X.

      Since I posted this question, I found a PHP script that does a download (example that comes with files, etc.) and it worked through the browser. It seems that perl is at issue, and all the headers are just not working.

      And I tried the content disposition and content type together, and individually, and the same result happens: it prints the output to the browser.

      Any tips on how to configure the server?

      Thank you.

        What aitap said sounds like it might be right to me. Webservers can be (Apache often is?) set up to send their own headers and since headers can only go once your CGI headers are printed as content.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1020908]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2018-03-17 06:35 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (223 votes). Check out past polls.