(English) 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 MIME types. Either the MIME type wasn't being set correctly or their browser was ignoring it. In any case the consequence was that the byte stream was being output to screen as garbled (Greek numbers, according to the client) text.

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 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 cookies generated by the POST to a text file. Then, using the text file 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

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.

Deixa un comentari

L'adreça electrònica no es publicarà Els camps necessaris estan marcats amb *

*

Podeu fer servir aquestes etiquetes i atributs HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>