Skip to content

Instantly share code, notes, and snippets.

@Hallmanac
Last active February 18, 2017 19:35
Show Gist options
  • Save Hallmanac/caceaec63d7029378cceeb9843a1c073 to your computer and use it in GitHub Desktop.
Save Hallmanac/caceaec63d7029378cceeb9843a1c073 to your computer and use it in GitHub Desktop.
Creating accept header interception for $http in Angular
/** @ngInject */
function _getHeaderInterceptor(utils: Nebbia.IUtils) {
const interceptor: ng.IHttpInterceptor = {
request: (config: ng.IRequestConfig) => {
const isGet = utils.stringContains("GET", config.method, false);
const hasJsonAcceptType = utils.stringContains("/json", config.headers["Accept"], false);
const isHtmlFileRequest = _isHtmlUrl();
if (isGet && isHtmlFileRequest && hasJsonAcceptType) {
config.headers["Accept"] = "application/html, text/html, */*";
}
return config;
function _isHtmlUrl(): boolean {
const index = config.url.lastIndexOf(".");
if (index === -1) {
return false;
}
const fileExt = config.url.slice(index);
const isHtml = fileExt === "html";
return isHtml;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment