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

header Content-Type in request without a body #524

Closed
Philippe-Torrelli-Ale opened this issue Mar 10, 2022 · 1 comment · Fixed by #704
Closed

header Content-Type in request without a body #524

Philippe-Torrelli-Ale opened this issue Mar 10, 2022 · 1 comment · Fixed by #704
Assignees
Milestone

Comments

@Philippe-Torrelli-Ale
Copy link

Philippe-Torrelli-Ale commented Mar 10, 2022

Hello,

I just fell into a problem calling a rest api that returned an error on a GET request, because Resty added a header Content-Type: application/json in my GET request. (no idea why they check that, and fortunately the returned error was explicit)

my code looks like:
reponse, err := client.R().SetPathParams(pathparams).SetQueryParams(params).SetResult(typ).Get( url)

So I added a "middleware" function

noContentTypeIfNotNeeded` := func(c *resty.Client, req *resty.Request) error {
 switch req.Method {
  case "GET", "DELETE":
    req.Header["Content-Type"] = nil
  }
  return nil
}
client.OnBeforeRequest(noContentTypeIfNotNeeded)

It does it for me, which is great, but not sure the current behiavour is legit..
It's not forbidden by the rfc but afaik this header should only be present in requests sending a body,
i'm not sure if it is an issue or not, (and i'm way too shallow on http to debate that, but I thought you might be interested in case no one reported it before.

Also, in resty traces i see "Content-Type:", so i'm not sure if my content-type header is sent or not by the library..
nor if it is the right way to remove an unwanted header.
I tried to replace the req.Header[...]=nil with a delete, but it didn't work.

@jeevatkm jeevatkm self-assigned this Sep 24, 2023
@jeevatkm jeevatkm added this to the v2.9.0 milestone Sep 24, 2023
@jeevatkm
Copy link
Member

@Philippe-Torrelli-Ale Thanks for reporting; I have added a specific test case for the issue, but I'm unable to reproduce the reported issue.

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

Successfully merging a pull request may close this issue.

2 participants