Only try to parse JSON documents

This patch adds code to only parse the HTTP response body as JSON if the
content-type header advertises the content as JSON. In my case, the
directory server was unavailable: it returned a 503 HTTP response code
with an HTML document, and the only thing lego reported was:

    2016/04/04 19:12:56 Could not create client: get directory at 'https://acme-v01.api.letsencrypt.org/directory': invalid character '<' looking for beginning of value

This was caused by trying to parse the document body (HTML) as JSON,
without looking at the content-type header and returning the JSON parse
error.
This commit is contained in:
Alexander Neumann 2016-04-04 23:15:49 +02:00
parent ca19a90028
commit ec667a7ed1

View file

@ -52,11 +52,15 @@ func (c challengeError) Error() string {
func handleHTTPError(resp *http.Response) error { func handleHTTPError(resp *http.Response) error {
var errorDetail RemoteError var errorDetail RemoteError
// try to decode the content as JSON
if resp.Header.Get("Content-Type") == "application/json" {
decoder := json.NewDecoder(resp.Body) decoder := json.NewDecoder(resp.Body)
err := decoder.Decode(&errorDetail) err := decoder.Decode(&errorDetail)
if err != nil { if err != nil {
return err return err
} }
}
errorDetail.StatusCode = resp.StatusCode errorDetail.StatusCode = resp.StatusCode