Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request a file with a custom header

I have an unusual requirement. Essentially I need a way so that, when the user clicks on a link or button, they will receive a PDF. The tricky part here is that the server won't process the request at all unless a custom header is sent with it (otherwise it deems the person logged out and sends them to the login screen).

At the moment the way the header works cannot be changed so please don't dwell on it; it will get changed in the future and is an internal application that I have no control over.

The options I have explored:

  • Using an iframe or simply opening a new window with some sort of path that will return the PDF. This can't work because I cannot specify the required header for the PDF and would be redirected before reaching the PDF itself.
  • Using a form and submitting the request can't work because I can't add any custom headers to forms (only XHR and plugins can, AFAIK).
  • Using XHR can't work because, while it can add the header and retrieve the file, there is no way to save it on the client side.

It would appear my only options at this point are essentially:

  • Use some sort of plugin such as Flash or Silverlight to request the file.
  • Force the change of the requirement much earlier than expected so that a header is no longer required.

Is there anything I am missing here? I'm hoping someone can either verify my findings or point me to something I missed because, as far as I can tell, there isn't really anything I can do here.

EDIT: This seems apt and confirms what I was thinking: XMLHttpRequest to open PDF in browser

like image 279
Kris Avatar asked May 09 '12 12:05

Kris


People also ask

How do I create a custom header in request?

Under Custom response headers, click Add header. Enter the Header name and Header value for the custom response header. Enter any additional custom response headers. Click Save.

What is HTTP request custom header?

Custom HTTP headers can be used to filter requests or specify a value for the Accept header. Some endpoints employ custom HTTP headers to filter data returned by a GET or POST request.

How do I change the request header?

After installing the extension, Go to app.requestly.io/rules.Then create rule and select "Modify Headers". After that give the rule name, and from here you can add, override and remove headers by providing a Header name and value for that header.


1 Answers

Tested to work in chrome:

function toBinaryString(data) {
    var ret = [];
    var len = data.length;
    var byte;
    for (var i = 0; i < len; i++) { 
        byte=( data.charCodeAt(i) & 0xFF )>>> 0;
        ret.push( String.fromCharCode(byte) );
    }

    return ret.join('');
}


var xhr = new XMLHttpRequest;

xhr.open( "GET", "/test.pdf" ); //I had test.pdf this on my local server


xhr.addEventListener( "load", function(){
    var data = toBinaryString(this.responseText);
    data = "data:application/pdf;base64,"+btoa(data);
    document.location = data;
}, false);

xhr.setRequestHeader("magic", "header" );
xhr.overrideMimeType( "application/octet-stream; charset=x-user-defined;" );
xhr.send(null);

You can change application/pdf to application/octet-stream to have download prompt. But it's pretty easy to download from the chrome's reader as well.

In firefox nothing happens I guess it's because I don't have a plugin to deal with application/pdf installed. Changing to application/octet-stream will prompt a dl.

With IE I suppose you need some kind of VBScript/ActiveX hackery

If the file is huge, using data uri might crash the browser, in that case you can use BlobBuilder and Object URLs.

like image 189
Esailija Avatar answered Oct 13 '22 23:10

Esailija