Skip to content

Conversation

@simitt
Copy link
Contributor

@simitt simitt commented Nov 6, 2018

fixes #1514

V2 Endpoints now return proper content-type and content in proper format. On Success no body is returned so no content-type is set. On Failure content type is set to application/json if the Accept header specifically requests json or it allows any type. Otherwise it is set to text/plain; charset=utf-8.

Details to the bugfix: The WriteHeader needs to be called after all other headers have been set, but before the actual Write method is called. According to the WriteHeader docs there are two points to consider:

  • Changing the header map after a call to WriteHeader (or Write) has no effect unless the modified headers are trailers.
  • If WriteHeader is not called explicitly, the first call to Write will trigger an implicit WriteHeader(http.StatusOK).

buf, err := json.Marshal(sr)
if err != nil {
logger.Errorw("error sending response", "error", err)
setContentType(w, r)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just some ideas, but instead of setContentType could we simplify by changing sendJSON and sendPlain to take statusCode int and then set the content type inside each one. Maybe we could also change sendJSON to take an interface{} instead and then use that both in v1 and v2.

Then, instead of having a sendStatus in common_handlers.go which is only used in v1 (AFAICT), we could perhaps create one that we can use from both v1 and v2:

func sendResponse(w http.ResponseWriter, r *http.Request, statusCode int, response interface{}) {
    if acceptsJSON(r) {
         sendJSON(w, r, statusCode, response)
         return
    }
    sendPlain(w, r, statusCode, response)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a couple of differences between v1 and v2 (in v2 Content-Type is only set on errors, counter for errors is increased in different places, the returned error messages are not the same). So I could align that and change some behaviour in v1, but I wasn't aiming for any change in v1, especially not within a bugfix.
I think we should refactor that when we remove v1, but if you feel strongly about it I can come up with something.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure @simitt 👍

@simitt simitt merged commit 8d54c3f into elastic:master Nov 6, 2018
simitt added a commit to simitt/apm-server that referenced this pull request Nov 6, 2018
simitt added a commit that referenced this pull request Nov 6, 2018
simitt added a commit to simitt/apm-server that referenced this pull request Nov 6, 2018
simitt added a commit that referenced this pull request Nov 6, 2018
@simitt simitt deleted the 1514-fix-content-type branch November 8, 2018 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong response Content-Type for /intake/v2/events

2 participants