RangelReale/osin — pull request #13 by joeshaw (2014-05-06 14:26:31+00:00)
Title: Writing status text breaks JSON decoding

`OutputJSON` as of 3cfb7ff0e06c20ad7c655eaf309d56bb8dd9ca6b has the following chunk of code:

```go
// 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.