Jaycean commented on a change in pull request #1465:
URL: https://github.com/apache/apisix-dashboard/pull/1465#discussion_r578541968



##########
File path: api/internal/handler/route_online_debug/route_online_debug.go
##########
@@ -63,66 +66,87 @@ type Result struct {
        Data    interface{} `json:"data,omitempty"`
 }
 
-func DebugRequestForwarding(c droplet.Context) (interface{}, error) {
+func (h *Handler) DebugRequestForwarding(c droplet.Context) (interface{}, 
error) {
        //TODO: other Protocols, e.g: grpc, websocket
-       paramsInput := c.Input().(*ParamsInput)
-       requestProtocol := paramsInput.RequestProtocol
-       if requestProtocol == "" {
-               requestProtocol = "http"
+       input := c.Input().(*DebugOnlineInput)
+       protocol := input.RequestProtocol
+       if protocol == "" {
+               protocol = "http"
        }
 
        protocolMap := make(map[string]ProtocolSupport)
        protocolMap["http"] = &HTTPProtocolSupport{}
        protocolMap["https"] = &HTTPProtocolSupport{}
 
-       if v, ok := protocolMap[requestProtocol]; ok {
-               return v.RequestForwarding(c)
-       } else {
-               return &data.SpecCodeResponse{StatusCode: 
http.StatusBadRequest},
-                       fmt.Errorf("Protocol unsupported %s, only http or https 
is allowed, but given %s", paramsInput.RequestProtocol, 
paramsInput.RequestProtocol)
+       if v, ok := protocolMap[protocol]; ok {
+               ret, err := v.RequestForwarding(c)
+               return ret, err
        }
+
+       return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest}, 
fmt.Errorf("protocol unspported %s", protocol)
 }
 
 type HTTPProtocolSupport struct {
 }
 
 func (h *HTTPProtocolSupport) RequestForwarding(c droplet.Context) 
(interface{}, error) {
-       paramsInput := c.Input().(*ParamsInput)
-       bodyParams := paramsInput.BodyParams
-       client := &http.Client{}
+       input := c.Input().(*DebugOnlineInput)
+       url := input.URL
+       method := input.Method
+       body := input.Body
+       contentType := input.ContentType
+
+       if url == "" || method == "" {
+               return &data.SpecCodeResponse{StatusCode: 
http.StatusBadRequest}, fmt.Errorf("parameters error")
+       }
 
+       client := &http.Client{}
        client.Timeout = 5 * time.Second
-       req, err := http.NewRequest(strings.ToUpper(paramsInput.Method), 
paramsInput.URL, strings.NewReader(bodyParams))
+
+       var tempMap map[string][]string
+       err := json.Unmarshal([]byte(input.HeaderParams), &tempMap)
+
+       if err != nil {
+               return &data.SpecCodeResponse{StatusCode: 
http.StatusInternalServerError}, err
+       }
+
+       req, err := http.NewRequest(strings.ToUpper(method), url, 
bytes.NewReader(body))
        if err != nil {
                return &data.SpecCodeResponse{StatusCode: 
http.StatusInternalServerError}, err
        }
-       for k, v := range paramsInput.HeaderParams {
+
+       req.Header.Add("Content-Type", contentType)
+       for k, v := range tempMap {
                for _, v1 := range v {
                        req.Header.Add(k, v1)
                }
        }
+
        resp, err := client.Do(req)
        if err != nil {
                return &data.SpecCodeResponse{StatusCode: 
http.StatusInternalServerError}, err
        }
-       defer resp.Body.Close()
+       defer func() {
+               if resp != nil {
+                       resp.Body.Close()
+               }
+       }()

Review comment:
       It can be used directly `resp.Body.Close ()` is OK. Correct reading will 
also close the transaction.
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to