I have the below class for talking to a REST API endpoint:
export class DocumentRESTClient {
/** Axios instance to be used by all derived classes */
readonly axiosInstance: AxiosInstance
public constructor(requiredEnvVars: APIEnvVars) {
const requestTimeout = parseInt(requiredEnvVars.API_REQUEST_TIMEOUT?.trim() || '120000', 10)
this.axiosInstance = axios.create({
baseURL: requiredEnvVars.BASE_API_URL,
timeout: requestTimeout,
})
this.axiosInstance.interceptors.request.use(request => {
console.log('---------------- axois interceptor -----------------')
console.log('Starting Request', JSON.stringify(request, null, 2))
return request
})
axiosRetry(this.axiosInstance, {
retries: 10,
retryCondition: (error) => error?.response?.status === 429,
})
}
public async postData(uri: string, data: any, headerParams: Record<string, string>, baseUrl?: string): Promise<any> {
console.log('postData data:', data)
return await this.axiosInstance.post(uri, data, { headers: headerParams, baseURL: baseUrl })
}
public async getData(
uri: string,
params: Record<string, unknown>,
headerParams: Record<string, string>
): Promise<any> {
return await this.axiosInstance.get(uri, { headers: headerParams, data: qs.stringify(params) })
}
}
When I run this, the code is failing, hence I added an interceptor to see what is happening inside. It prints the below:
-------------- authorize --------------------
postData data: URLSearchParams { 'grant_type' => 'client_credentials', 'scope' => 'https://api-stg.xxx.com/.default' }
---------------- axois interceptor -----------------
Starting Request {
"transitional": {
"silentJSONParsing": true,
"forcedJSONParsing": true,
"clarifyTimeoutError": false
},
"transformRequest": [
null
],
"transformResponse": [
null
],
"timeout": 120000,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"maxBodyLength": -1,
"headers": {
"common": {
"Accept": "application/json, text/plain, */*"
},
"delete": {},
"get": {},
"head": {},
"post": {
"Content-Type": "application/x-www-form-urlencoded"
},
"put": {
"Content-Type": "application/x-www-form-urlencoded"
},
"patch": {
"Content-Type": "application/x-www-form-urlencoded"
},
"Authorization": "Basic NTQwNGM2.................M",
"Content-Type": "application/x-www-form-urlencoded"
},
"baseURL": "https://api-stg.xxx.com",
"method": "post",
"url": "/oauth2/token",
"data": {},
"axios-retry": {
"retryCount": 0,
"lastRequestTime": 1692977951412
}
}
Seems that data
is empty inside the interceptor. Why is that happening? Where are grant_type
and scope
? Please advise.