From 45c297b5277c7f6659d17debe5179e407c46751e Mon Sep 17 00:00:00 2001 From: Heng Lu Date: Thu, 17 Aug 2023 14:14:53 +0800 Subject: [PATCH] fix panic when export as oav --- formatter/oav_traffic.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/formatter/oav_traffic.go b/formatter/oav_traffic.go index 79cc091..73609a3 100644 --- a/formatter/oav_traffic.go +++ b/formatter/oav_traffic.go @@ -32,26 +32,35 @@ type LiveResponse struct { func (o OavTrafficFormatter) Format(r types.RequestTrace) string { var requestBody interface{} - err := json.Unmarshal([]byte(r.Request.Body), &requestBody) - if err != nil { - requestBody = nil + requestHeaders := make(map[string]string) + if r.Request != nil { + err := json.Unmarshal([]byte(r.Request.Body), &requestBody) + if err != nil { + requestBody = nil + } + requestHeaders = r.Request.Headers } + var responseBody interface{} - err = json.Unmarshal([]byte(r.Response.Body), &responseBody) - if err != nil { - responseBody = nil + responseHeaders := make(map[string]string) + if r.Response != nil { + err := json.Unmarshal([]byte(r.Response.Body), &responseBody) + if err != nil { + responseBody = nil + } + responseHeaders = r.Response.Headers } out := OavTraffic{ LiveRequest: LiveRequest{ - Headers: r.Request.Headers, + Headers: requestHeaders, Method: r.Method, Url: r.Url, Body: requestBody, }, LiveResponse: LiveResponse{ StatusCode: fmt.Sprintf("%d", r.StatusCode), - Headers: r.Response.Headers, + Headers: responseHeaders, Body: responseBody, }, }