I am using angular 4 for one of my projects and i have a method which creates a form element dynamically and submits it
postToUrl(path, params, method) {
method = method || 'post';
let form = document.createElement('form');
form.setAttribute('method', method);
form.setAttribute('action', path);
for (let key in params) {
if (params && params.hasOwnProperty(key)) {
let hiddenField = document.createElement('input');
hiddenField.setAttribute('type', 'hidden');
hiddenField.setAttribute('name', key);
hiddenField.setAttribute('value', params[key]);
form.appendChild(hiddenField);
}
}
document.body.appendChild(form);
form.submit();
setTimeout(() => {
document.body.removeChild(form);
}, 2000);
}
What i want to do is set a custom header to the request so that i can attach all the headers required by the server. Is there any way i can write a common interceptor for this so that i don't have to repeat the lines. Please help. Any help is appreciated.