Browse Source

[FIX-4876]Http alert plugin fix (#4877)

* remove header in  httpRequest

* codestyle fix

Co-authored-by: 李巨丰 <lijf@2345.com>
JuFeng Li 4 years ago
parent
commit
a0a2c9141b

+ 10 - 4
dolphinscheduler-alert-plugin/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSender.java

@@ -26,6 +26,7 @@ import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.client.methods.HttpRequestBase;
 import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClientBuilder;
 import org.apache.http.impl.client.HttpClientBuilder;
 import org.apache.http.util.EntityUtils;
 import org.apache.http.util.EntityUtils;
@@ -110,17 +111,17 @@ public class HttpSender {
     }
     }
 
 
     private void createHttpRequest(String msg) {
     private void createHttpRequest(String msg) {
-
         if (REQUEST_TYPE_POST.equals(requestType)) {
         if (REQUEST_TYPE_POST.equals(requestType)) {
             httpRequest = new HttpPost(url);
             httpRequest = new HttpPost(url);
+            setHeader();
             //POST request add param in request body
             //POST request add param in request body
             setMsgInRequestBody(msg);
             setMsgInRequestBody(msg);
         } else if (REQUEST_TYPE_GET.equals(requestType)) {
         } else if (REQUEST_TYPE_GET.equals(requestType)) {
             //GET request add param in url
             //GET request add param in url
             setMsgInUrl(msg);
             setMsgInUrl(msg);
             httpRequest = new HttpGet(url);
             httpRequest = new HttpGet(url);
+            setHeader();
         }
         }
-        setHeader();
     }
     }
 
 
     /**
     /**
@@ -156,11 +157,16 @@ public class HttpSender {
     /**
     /**
      * set body params
      * set body params
      */
      */
-    private String setMsgInRequestBody(String msg) {
+    private void setMsgInRequestBody(String msg)  {
         ObjectNode objectNode = JSONUtils.parseObject(bodyParams);
         ObjectNode objectNode = JSONUtils.parseObject(bodyParams);
         //set msg content field
         //set msg content field
         objectNode.put(contentField, msg);
         objectNode.put(contentField, msg);
-        return objectNode.toString();
+        try {
+            StringEntity entity = new StringEntity(bodyParams, DEFAULT_CHARSET);
+            ((HttpPost)httpRequest).setEntity(entity);
+        } catch (Exception e) {
+            logger.error("send http alert msg  exception : {}", e.getMessage());
+        }
     }
     }
 
 
 }
 }