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

setting 'accept' header if 'content-type' header is set to JSON or XML type #643

Open
Dylmay opened this issue Apr 2, 2023 · 2 comments

Comments

@Dylmay
Copy link

Dylmay commented Apr 2, 2023

The 'accept' header is set if the 'content-type' header is a JSON or XML type which causes unexpected behavior if creating a request like here, where the underlying spec requires a JSON type but the server is not expected to return the same type, or any type, back.

Is there reasoning to set the 'accept' header automatically if it is a JSON or XML type? It seems like the auto-setting of the accept header should be toggle-able by client or the 'accept' header should only auto-set for basic MIME types.

@jeevatkm
Copy link
Member

@Dylmay Typically, resty understands the JSON and XML marshal/unmarshals. Hence, it notifies the server via the Accept header if it sees content type JSON/XML and the Accept header is empty.

resty/middleware.go

Lines 129 to 132 in e12ab69

if IsStringEmpty(hdr.Get(hdrAcceptKey)) && !IsStringEmpty(ct) &&
(IsJSONType(ct) || IsXMLType(ct)) {
hdr.Set(hdrAcceptKey, hdr.Get(hdrContentTypeKey))
}

So, if the user sets the Accept header explicitly, then Resty will not do any action.

@Greyh4t
Copy link

Greyh4t commented Dec 12, 2023

@Dylmay Typically, resty understands the JSON and XML marshal/unmarshals. Hence, it notifies the server via the Accept header if it sees content type JSON/XML and the Accept header is empty.

resty/middleware.go

Lines 129 to 132 in e12ab69

if IsStringEmpty(hdr.Get(hdrAcceptKey)) && !IsStringEmpty(ct) &&
(IsJSONType(ct) || IsXMLType(ct)) {
hdr.Set(hdrAcceptKey, hdr.Get(hdrContentTypeKey))
}

So, if the user sets the Accept header explicitly, then Resty will not do any action.

I have encountered the same issue as well. In principle, resty should not set any unexpected header for the user. Users are supposed to set headers themselves, and in some cases, it is necessary not to set certain headers. In the current mode, if users want to avoid setting the User-Agent or Accept header, there is no way to do so.

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

No branches or pull requests

3 participants