2

I am using Axios for the API call, here I have created the instance for a common header in each and every API call(authorization things).

export const axiosInstance = axios.create({
  baseURL: 'demo API'
})

axiosInstance.interceptors.request.use(function (config) {
    config.headers['Accept'] = 'application/json'
    config.headers['testkey'] = 'randomdata'

  return config
}, function (err) {
  return Promise.reject(err)
})

now for calling

return axiosInstance.post('/api/demo, {myCommonBody})
  .then(data => {
   //action and action..
 })
  .catch(err => {
    throw err;
  }); 

Here all I have to pass myCommonBody in all API body.

But such things I need to be common from the interceptor as well (alike in the header I did).

So whenever we call any API with common API instance, here we get the BODY part by default.

Any lead appreciates for the answer. Thanks.

Anupam Maurya
  • 1,927
  • 22
  • 26

2 Answers2

3

axiosInstance.interceptors.request.use(function (config) {
    config.headers['Accept'] = 'application/json'
    config.headers['testkey'] = 'randomdata'

    config.data = Object.assign({}, config.data, {myCommonBody});

  return config
}, function (err) {
  return Promise.reject(err)
})

or

export const axiosInstance = axios.create({
  baseURL: 'demo API',
  data: {myCommonBody}
})
SIMDD
  • 615
  • 5
  • 15
0
export const axiosInstance = axios.create({
  baseURL: 'demo API',
  data: {'myCommonBody': 'myCommonBodyDetails')
})

And all set for every api call.

Anupam Maurya
  • 1,927
  • 22
  • 26