Skip to content

Commit 682b3d1

Browse files
committed
detect binary responses
1 parent 81782b4 commit 682b3d1

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

render/http.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package render
33
import (
44
"encoding/json"
55
"fmt"
6+
"net/http"
67
"os"
8+
"strings"
79
"sync"
810
"time"
911

@@ -138,14 +140,21 @@ func printHTTPResult(result checks.HttpTestResult) string {
138140
str += fmt.Sprintf(" Response Status Code: %v\n", result.StatusCode)
139141
str += " Response Body: \n"
140142
unmarshalled := map[string]interface{}{}
141-
err := json.Unmarshal([]byte(result.BodyString), &unmarshalled)
142-
if err == nil {
143-
pretty, err := json.MarshalIndent(unmarshalled, "", " ")
143+
bytes := []byte(result.BodyString)
144+
145+
contentType := http.DetectContentType(bytes)
146+
if contentType == "application/json" || strings.HasPrefix(contentType, "text/") {
147+
err := json.Unmarshal([]byte(result.BodyString), &unmarshalled)
144148
if err == nil {
145-
str += string(pretty)
149+
pretty, err := json.MarshalIndent(unmarshalled, "", " ")
150+
if err == nil {
151+
str += string(pretty)
152+
}
153+
} else {
154+
str += result.BodyString
146155
}
147156
} else {
148-
str += fmt.Sprint(result.BodyString)
157+
str += fmt.Sprintf("Binary %s file", contentType)
149158
}
150159
}
151160
str += "\n"

0 commit comments

Comments
 (0)