question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

config.data has been changed in response interceptor

See original GitHub issue

I am using axios request and response interceptors but it seems response.config.data is not an Object.

Here is my Request interceptor:

config.data is an instance of my Form Object

API.interceptors.request.use((config) => {
  if (config.data instanceof Form) {
    config.data.startProcessing()
  }
  return config
})

Now going to the response interceptor, it seems it is not a Form anymore when it is a string already.

API.interceptors.response.use((response) => {
  // this response.config.data is now a string not my Form object anymore
  if (response.config.data instanceof Form) {
    response.config.data.finishProcessing()
  }
  return response
})

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6

github_iconTop GitHub Comments

3reactions
liveKangcommented, May 15, 2018

so just try this in node-module/axios/lib/defaults.js,change this Fun.

transformResponse: [function transformResponse(data) {
    /*eslint no-param-reassign:0*/
    if (typeof data === 'string') {
      try {
        if (!String.prototype.trim) {
          String.prototype.trim = function () {
            return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
          };
        }
        data = JSON.parse(data.trim(''));
      } catch (e) { 
        /* Ignore */
        console.log('can not JSON.parse the response', e)
      }
    }
    return data;
  }],

hope this can help you.

0reactions
chinesedfancommented, Jan 26, 2020

The reason is that config.data has been changed in response interceptor, as well as my edited issue title. A workaround can be saving the original data in a customized field of the config.

API.interceptors.request.use((config) => {
  config.rawData = config.data
  return config
})

API.interceptors.response.use((response) => {
  // read `config.rawData`
  return response
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Error using axios interceptors and responses - Stack Overflow
I want to intercept requests and responses and add the user's token. When I try to use the request interceptor, I get the...
Read more >
config | API Reference | ArcGIS Maps SDK for JavaScript 4.25
Makes changes to the response after the request is sent, but before it's returned to the caller. Parameter. response RequestResponse. The response object....
Read more >
Intercepting Requests & Responses Using Axios
The steps to create Axios request & response interceptors are: Create a new Axios instance with a custom config; Create request, response & ......
Read more >
10 Using Service Interceptors to Manipulate Requests
When the request returns from the application, the data can be changed as the request returns through the invocation chain. This is useful...
Read more >
Making HTTP requests with Axios - CircleCI
Axios is a promise-based HTTP library that lets developers make ... should display a response after the Axios request has been made and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found