From a74f75824ca8caed889cc8b1b11f2c6c97036820 Mon Sep 17 00:00:00 2001 From: nitesh3108 Date: Mon, 12 Feb 2024 03:20:35 -0500 Subject: [PATCH] parsing html json --- api/api.go | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/api/api.go b/api/api.go index b5c8e8e..d05648a 100755 --- a/api/api.go +++ b/api/api.go @@ -610,21 +610,7 @@ func (c *client) GetReferer() string { func parseJSONHTMLError(r *http.Response) error { // check the content type of the response - contentType := r.Header.Get("Content-Type") - switch { - case strings.HasPrefix(contentType, "application/json"): - jsonErr := &JSONError{} - // decode JSON error response - err := json.NewDecoder(r.Body).Decode(jsonErr) - if err != nil { - return err - } - jsonErr.StatusCode = r.StatusCode - if len(jsonErr.Err) > 0 && jsonErr.Err[0].Message == "" { - jsonErr.Err[0].Message = r.Status - } - return jsonErr - case strings.HasPrefix(contentType, "text/html"): + if r.Header.Get("Content-Type") == "text/html" { htmlError := &HTMLError{} // decode HTML error response doc, err := goquery.NewDocumentFromReader(r.Body) @@ -641,9 +627,18 @@ func parseJSONHTMLError(r *http.Response) error { htmlError.Message = doc.Find("title").Text() } return htmlError - default: - // Unexpected content type - return fmt.Errorf("unexpected content type: %s", contentType) + } else { + jsonErr := &JSONError{} + // decode JSON error response + err := json.NewDecoder(r.Body).Decode(jsonErr) + if err != nil { + return err + } + jsonErr.StatusCode = r.StatusCode + if len(jsonErr.Err) > 0 && jsonErr.Err[0].Message == "" { + jsonErr.Err[0].Message = r.Status + } + return jsonErr } }