I have a simple html5 player, implemented with videoJS. In order to properly retrieve the source files from the server, I need to set a custom header in the request for the video.
Since the application uses AngularJS, I implemented an Interceptor to set the header:
myApp.factory('headerInterceptor', function () {
return {
request: function (config) {
config.headers['my-header'] = 'test';
return config;
}
};
});
myApp.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push('headerInterceptor');
}
Problem with this, is that the call to the video is not catched by it, so no header is set (it is though for other resources). So angular does not load the videos. No big surprise in that. Checking the Network tab in developer tools, found out that videoJS initiates the call:
But finding my way in the videoJS plugin has been difficult, and couln't find where the calls are made. Im just wondering, is there a simple way to set the header for this call? doesn't matter if it's plain javascript, or through angular, or even modifying videoJS plugin.
Another example of using a custom HTTP header would be to implement the X-Pull header. You can use this custom header for a variety of purposes including rate limiting bandwidth on your origin server, restricting CDN traffic, creating custom logic on your origin server, etc.
HTTP headers let the client and the server pass additional information with an HTTP request or response. An HTTP header consists of its case-insensitive name followed by a colon (: ), then by its value. Whitespace before the value is ignored. Custom proprietary headers have historically been used with an X- prefix,...
Initially, it was recommended to begin naming custom headers with X- so that users would be aware that these headers were custom and not standardized. However, according to RFC 6648, this recommendation has since been deprecated.
It is a request-type header. It is used to identify the original host requested by the client in the Host HTTP request header. It is an request-type header. It is used to identifying the protocol that the client used to connect with a proxy or load balancer. It can be HTTP or HTTPS.
Inside an Angular run block, you can override the videojs plugin XHR beforeRequest function. YMMV, but if you use HLS plugin for example, you could do something like that:
angular.module('app', []).run(function($localStorage) {
var _beforeRequest = videojs.Hls.xhr.beforeRequest;
videojs.Hls.xhr.beforeRequest = function(options) {
if (_.isFunction(_beforeRequest)) {
options = _beforeRequest(options);
}
if ($localStorage.token) {
options.headers = options.headers || {};
options.headers.Authorization = 'Token ' + $localStorage.token;
}
return options;
};
});
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