I work in vue.js and I'm using the axios to make requests to the server. I have configured an interceptor to add authorizaton tokens to requests. Function which generates the token needs complete url with protocol to create the proper token. I have created my axios instance http.
If I use complete url in http call: http.get('http://ediscore.net/api/config/category'
it works well. But if I set the base URL at the axios level:const http = axios.create({baseURL: 'http://ediscore.net/api',
and in axios call I use only http.get('config/category')
I have a problem. The url from interceptor's property is not complete.
My interceptor:
http.interceptors.request.use(
(config: AxiosRequestConfig) => {
console.log('Axios interceptor => config: ', config);
console.log('Axios interceptor => config.url: ', config.url);
const token: string = authService.getHmacToken(config.url, config.method);
if (token) {
config.headers.Authorization = `${token}`;
}
return config;
},
(error: AxiosError) => Promise.reject(error)
);
console.log() of the interceptor's config:
Axios interceptor => config: {adapter: ƒ, transformRequest: {…}, transformResponse: {…}, timeout: 20000, xsrfCookieName: "XSRF-TOKEN", …}
adapter: ƒ xhrAdapter(config)
baseURL: "http://ediscore.net/api"
data: undefined
headers: {Accept: "application/json", Authorization: ""...
maxContentLength: -1
method: "get"
timeout: 20000
transformRequest: {0: ƒ}
transformResponse: {0: ƒ}
url: "http://ediscore.net/api/config/category"
validateStatus: ƒ validateStatus(status)
xsrfCookieName: "XSRF-TOKEN"
xsrfHeaderName: "X-XSRF-TOKEN"
__proto__: Object
console.log() of the interceptor's config.url property:
Axios interceptor => config.url: config/category
How is it possible that in the entire config object I can see property url: http://ediscore.net/api/config/category
(complete, including protocol - see the line 11)
but if I list only its property url, I get only config/category
? (This is the value I send to the token generator and that's why it doesn't work because it's missing the baseURL part).
It makes no sense to me. Please help because I don't understand.