I have angularjs application where users enter data that is saved to database, then on server side it is compiled into pdf file. All access requires appropriate authentication headers in place. After needed data is filled a user presses button to save data and then to retrieve pdf file. Optimally, I call $window.open(url_generating_pdf
) in my angularjs app. This works well and opens in another window, but how to add authentication header to this $window
request? In my understanding I cannot download pdf, and print it with ajax, so I am missing this authentication. Or would there be other ways to call url from server, and make the file open in another window?
@sureshvv — No. You cannot control the headers sent there either. If it preserves login status that is because the login method doesn't use custom headers.
The token is a text string, included in the request header. In the request Authorization tab, select Bearer Token from the Type dropdown list. In the Token field, enter your API key value. For added security, store it in a variable and reference the variable by name.
I think I should update this old question with a correct and secure answer.
You can not add any headers in the HTTP GET request performed by window.open.
The secure way to make an authenticated request is to set the authentication token into a request header, and avoid exposing it into the URL, as my previous answer suggested (I have learned a some things since then).
To download a PDF (or any other binary data) from an authenticated server with AngularJS you should:
I hope this answer to clear doubts on this question and provides a secure way to download a resource from an authenticated REST API.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With