From ec667a7ed1f5a834b103450d3f912d8c41b08e5b Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Mon, 4 Apr 2016 23:15:49 +0200 Subject: [PATCH] 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. --- acme/error.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/acme/error.go b/acme/error.go index b32561a3..0d901123 100644 --- a/acme/error.go +++ b/acme/error.go @@ -52,10 +52,14 @@ func (c challengeError) Error() string { func handleHTTPError(resp *http.Response) error { var errorDetail RemoteError - decoder := json.NewDecoder(resp.Body) - err := decoder.Decode(&errorDetail) - if err != nil { - return err + + // try to decode the content as JSON + if resp.Header.Get("Content-Type") == "application/json" { + decoder := json.NewDecoder(resp.Body) + err := decoder.Decode(&errorDetail) + if err != nil { + return err + } } errorDetail.StatusCode = resp.StatusCode