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

Why remote the content-type when requestBody is empty ? #5814

Open
zWingz opened this issue Aug 10, 2023 · 9 comments
Open

Why remote the content-type when requestBody is empty ? #5814

zWingz opened this issue Aug 10, 2023 · 9 comments
Labels
priority::low A low priority issue

Comments

@zWingz
Copy link

zWingz commented Aug 10, 2023

Describe the issue

I have set headers: { 'content-type': 'application/json' }. But It can't be send to the server.
And I found the code https://github.com/axios/axios/blob/v1.x/lib/adapters/xhr.js#L195 will remove the content-type when the request body is empty.

Example Code

No response

Expected behavior

Still send the content-type to the server ?

Axios Version

1.3.3

Adapter Version

xhr

Browser

chrome

Browser Version

latest

Node.js Version

No response

OS

No response

Additional Library Versions

No response

Additional context/Screenshots

No response

@NikoRaisanen
Copy link

Hey zWingz, what's the use case for defining a content type if there is no content?

@zWingz
Copy link
Author

zWingz commented Aug 25, 2023

企业微信截图_adf76f5a-6867-4f84-a2d5-7a7dd721dd4e @NikoRaisanen Spring Boot will check the `content-type` So I have to set `content-type` even if there is no content in body

@jasonsaayman
Copy link
Member

may want to check the RFC https://www.rfc-editor.org/rfc/rfc7231#section-4.3.3 it is accepted that a post request would at least have a body of {} if it was nothing and not just no body at all

@jasonsaayman jasonsaayman added priority::low A low priority issue type::question and removed type::bug labels Sep 5, 2023
@prajwalbandak
Copy link

Hey @NikoRaisanen , would love to work on this issue. Can you please assign to me?

@tlevesque-ueat
Copy link

I have the opposite problem. When I try to send a POST with no body, the request is sent with a Content-Type of application/x-www-form-urlencoded. This value is never set explicitly in my code, I don't know where it comes from. And I can't find a way to completely suppress the header. Explicitly setting it to undefined has no effect. Setting it to null or an empty string sends an empty header. Is there a way to remove it?

@DigitalBrainJS
Copy link
Collaborator

DigitalBrainJS commented Mar 13, 2024

@tlevesque-ueat

Is there a way to remove it?

Just set Content-Type to false

@tlevesque-ueat
Copy link

Just set Content-Type to false

As far as I can tell, it does the same thing as setting it to null or an empty string (sends an empty header).
Doesn't seem to be a problem in my scenario, though, so I'll stick to that. Thanks

@DigitalBrainJS
Copy link
Collaborator

DigitalBrainJS commented Mar 13, 2024

@tlevesque-ueat The false value completely removes the header from the set and prevents Axios from setting it in the dispatch sequence.

@tlevesque-ueat
Copy link

Yes, I realize that now. The tool I was using to inspect the request (webhook.site) was showing an empty Content-Type, but it doesn't seem to actually be present in the request. Thanks!

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

No branches or pull requests

7 participants