So I want to modify a request header via my interceptor, however when I set the content type of a request the request function stops without any error.
The function stops at the line config.headers['Content-Type'] = 'application/json';
I'm thinking it's because the property 'Content-Type' doesn't exist on the object header but i'm pretty sure it's possible to add properties to an object like this in js.
const requestLogin = async (loginDTO) => {
let requestOptions = {
method: 'POST',
body: JSON.stringify(loginDTO)
};
return await fetch(`${baseURL}/Account/Login`, requestOptions);
}
request: function (url, config) {
// Modify the url or config here
if (!config) return;
config.headers['Content-Type'] = 'application/json';
return [url, config];
},
You can set config.headers['Content-Type'] if config.headers is already an object. If it is not then you get a TypeError.
Try to write a more strict guard:
if (!config || !config.headers || typeof config.headers !== 'object') return;
config.headers['Content-Type'] = 'application/json';
return [url, config];