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

feat: set content-type only when it doesn't exist on user custom headers #379

Merged
merged 2 commits into from Dec 13, 2023

Conversation

simpleapples
Copy link
Contributor

Hi @guonaihong

When I use gout to send a JSON like the code below.

err = g.POST(url)
    .SetHeader(core.H{"Content-Type": "application/json"})
    .SetJSON("{\"key\":\"val\"}")
    .Do()

I found that the http headers gout sent likes this

Content-Type: "application/json,application/json"

Then I investigated the source code and found that on this line

r.addContextType(req)

gout adds Content-Type to headers whehter it exists or not. Which means if the user uses SetHeader('Content-Type', 'xxx') and SetJSON() in the same request. The Content-Type will have two values. Because the Add func called by SetJSON and SetHeader appends values to a key, not replace it. As the screenshot shows.

image

To avoid this situation. I believe we should check existence of Content-Type before setting it. And if the Content-Type exists, means users already set it by themeselves (for better visability or other purposes) or users want to use a different header value. gout shouldn't set it or replace it.

@guonaihong guonaihong merged commit 2521c68 into guonaihong:master Dec 13, 2023
6 checks passed
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

Successfully merging this pull request may close these issues.

None yet

2 participants