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

Writing status text breaks JSON decoding #13

Merged
merged 1 commit into from
Jun 7, 2014

Conversation

joeshaw
Copy link
Contributor

@joeshaw joeshaw commented May 6, 2014

OutputJSON as of 3cfb7ff has the following chunk of code:

        // Output json
        w.Header().Add("Content-Type", "application/json")
        w.WriteHeader(rs.StatusCode)
        if rs.IsError && rs.StatusText != "" {
            // write status text
            fmt.Fprintln(w, strings.Replace(rs.StatusText, "\n", " ", -1)) // remove any newlines
        }

        encoder := json.NewEncoder(w)
        err := encoder.Encode(rs.Output)
        if err != nil {
            return err
        }

Writing out rs.StatusText to the http.ResponseWriter w causes the body of the response to contain non-JSON data, and breaks client parsing. I don't understand the purpose of that code.

You can see this if you set config.ErrorStatusCode or response.ErrorStatusCode to anything other than http.StatusOK. Here's the difference in curl:

With the default ErrorStatusCode:

< HTTP/1.1 200 OK
< Content-Type: application/json
< Date: Tue, 22 Apr 2014 20:42:26 GMT
< Content-Length: 196
<
{"error":"invalid_request","error_description":"The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed."}

With a changed ErrorStatusCode:

< HTTP/1.1 400 Bad Request
< Content-Type: application/json
< Date: Tue, 22 Apr 2014 20:41:25 GMT
< Content-Length: 342
<
The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed.
{"error":"invalid_request","error_description":"The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed."}

Should that code block simply be removed? I'm happy to write up a patch for it, but I wanted to check with you first in case there's something I am not understanding.

It doesn't get appended to the HTTP status line, it is part of the
body.  This results in the body not being valid JSON.

Fixes issue openshift#13.
RangelReale added a commit that referenced this pull request Jun 7, 2014
Writing status text breaks JSON decoding
@RangelReale RangelReale merged commit 1a924bd into openshift:master Jun 7, 2014
@RangelReale
Copy link
Contributor

Yes you're right, I was thinking of replacing the "Bad Request" message, but looks like this message cannot be changed on the current go version.

@joeshaw
Copy link
Contributor Author

joeshaw commented Jun 8, 2014

Thank you!

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.

2 participants