Skip to content
This repository has been archived by the owner on Jun 26, 2021. It is now read-only.

[api] using io.WriteString for writing responses instead of http.ResponseWriter.Write(...) #41

Conversation

ryandeivert
Copy link
Contributor

to: @securityclippy
size: small

Changes

  • Now using io.WriteString to write http responses due to part of an answer found here:

    All in all, whenever you write string values, [it's] recommended to use io.WriteString() as it may be more efficient (faster).

  • Also updating how unexpected errors are reported to the user by using http.Error instead of attempting to write a custom error struct.

}

// Set the header type/status on success
respWriter.WriteHeader(http.StatusOK)
respWriter.Header().Set("Content-Type", "application/json")
Copy link
Contributor

Choose a reason for hiding this comment

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

@ryandeivert is there ever an instance when the content-type would not be json? Generally we want to try to make everything a json response, but if there's a use-case for non-json let me know. (the whole point being, why not set the header type once, no matter what)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Purposely, probably not :) the only time (by design) I think the content type will be anything other than json is if http.Error is called - but that would overwrite the json content type if it existed already. I'll make the change 👍

@ryandeivert ryandeivert force-pushed the ryandeivert-io-writestring-response branch 2 times, most recently from f16df3a to bcc801a Compare February 28, 2018 03:33
@ryandeivert ryandeivert force-pushed the ryandeivert-io-writestring-response branch from bcc801a to 586f15d Compare February 28, 2018 05:44
@mattjane-okta mattjane-okta merged commit 4e0d3c2 into OktaSecurityLabs:master Feb 28, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants