Check HTTP headers (specifically MIME type) for file downloads

A client called us up saying they had problems downloading files from our e-learning portal. After some back and forth on the phone I found out the problem was their browser and/or the files' MIME types. Either the MIME type wasn't being set correctly or their browser was ignoring it. In any case they were seeing a bunch of "Greek numbers" on their screen; the file contents were just being printed on-screen.

I wanted to check the HTTP headers the URL was returning, to make sure we were in fact setting the MIME type. Firebug shows the headers in the console and net tabs. In this case they weren't being displayed though, as the URL was initiating a download. Slightly stumped, I thought for a minute and then remembered cURL.

I've never used cURL, but have read much of it. I guessed it could maybe do the trick, so I set about finding out. After a cursory search I found out that

 $ curl -I -H http://URL

Would return the headers for the requested URL. The URL I was trying requires cookie-based authentication, though. After reading the manual I managed to log in via cURL, save the cookies in a text file, then request the file using the cookies to authenticate:

First I POSTed to the login form's action URL with the necessary parameters, while saving the generated cookies to a text file. Then, using the text file as a source for cookies, I requested the file URL and, voilà, got what I needed.

$ curl -c cookies.txt -L -d "username=<USER>&pass=<PASS>" http://URL/login.php
$ curl -c cookies.txt -b cookies.txt -L -I http://URL/file.pdf

My reaction. There were fireworks and bangs and colours and it was just amazing.

Resources

About johnny

Computers have interested me since I can remember and conveniently I studied computer science. I also enjoy performing in a local amateur theatre group and cycling. This is where I post solutions to problems I've had in the office or any other project, hopefully clearly enough to refer back to in the future.
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>