Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

With version 0.27.1 the request header will be lost, while 0.27.0 will not #4858

Closed
yangqixin123 opened this issue Jul 17, 2022 · 9 comments
Closed

Comments

@yangqixin123
Copy link

yangqixin123 commented Jul 17, 2022

When sending a post request using version 0.27.1, the request header will be lost, but 0.27.0 will not
It is written as follows:

class Header {};
let myHeaders = new Header();
myHeaders["X-Foo"] = "bar";
myHeaders["X-Test"] = "test";
myHeaders["X-Time"] = Date.now();
  
axios.post('http://cors-test.appspot.com/test', { name: 'foo' }, { headers: myHeaders })
.then((res) => {
       console.log('res---',res.config.headers);
 })

See test.zip for details.

Environment

  • Axios Version [0.27.1]
  • Browser [Chrome]
  • Browser Version [103.0.5060.114]
  • Node.js Version [14.14]
  • OS: [iOS 12.4]

WX20220717-225027@2x

test.zip

@techbirds
Copy link
Contributor

You can use plain objects for parameter definitions, like this:


const myHeaders = {}
myHeaders["X-Foo"] = "bar";
myHeaders["X-Test"] = "test";
myHeaders["X-Time"] = Date.now();

axios.post('http://cors-test.appspot.com/test', {
    name: 'foo'
  }, {
    headers: myHeaders
  })
  .then((res) => {
    console.log('res---', res.config.headers);
  })

@yangqixin123
Copy link
Author

But my project has been using the new Class writing method before. The official does not seem to mention that this writing method cannot be used in the new update.

@WillianAgostini
Copy link
Contributor

WillianAgostini commented Jul 18, 2022

Hello @yangqixin123

In this commit 910fdc1b1018, the method generateHTTPMethod

axios/lib/core/Axios.js

Lines 142 to 149 in 6b9b05b

return this.request(mergeConfig(config || {}, {
method: method,
headers: isForm ? {
'Content-Type': 'multipart/form-data'
} : {},
url: url,
data: data
}));
modified the behavior of this method, I believe that was not the intention.

I can commit by changing the way this merge happens and solving this problem.

@yangqixin123
Copy link
Author

Hello @yangqixin123

In this commit 910fdc1b1018, the method generateHTTPMethod

axios/lib/core/Axios.js

Lines 142 to 149 in 6b9b05b

return this.request(mergeConfig(config || {}, {
method: method,
headers: isForm ? {
'Content-Type': 'multipart/form-data'
} : {},
url: url,
data: data
}));

modified the behavior of this method, I believe that was not the intention.
I can commit by changing the way this merge happens and solving this problem.

@WillianAgostini WillianAgostini ok i will be very grateful

@WillianAgostini
Copy link
Contributor

@yangqixin123 , you can workaround returning casting variable myHeaders like a generic object.

  myHeaders = {...myHeaders};

@yangqixin123
Copy link
Author

@WillianAgostini I tried this and it's ok, but the company's previous production environment project does not want to be changed because of the upgrade of axios, I hope the official modification can be compatible with the previous usage

WillianAgostini added a commit to WillianAgostini/axios that referenced this issue Jul 20, 2022
WillianAgostini added a commit to WillianAgostini/axios that referenced this issue Jul 20, 2022
WillianAgostini added a commit to WillianAgostini/axios that referenced this issue Jul 20, 2022
WillianAgostini added a commit to WillianAgostini/axios that referenced this issue Jul 20, 2022
@WillianAgostini
Copy link
Contributor

@yangqixin123, I created an PR #4871.

@yangqixin123
Copy link
Author

@WillianAgostini ok i will be very grateful

jasonsaayman added a commit that referenced this issue Sep 26, 2022
Co-authored-by: Jay <jasonsaayman@gmail.com>
@jasonsaayman
Copy link
Member

Closing as it merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants