add http post error
This commit is contained in:
+14
-4
@@ -5,6 +5,7 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/labstack/gommon/log"
|
||||
"gzzn.com/mini/ciimsproxy/internal"
|
||||
)
|
||||
|
||||
@@ -32,6 +33,7 @@ func main() {
|
||||
r.POST("/send", sendMessage)
|
||||
r.POST("/receive", receiveMessage)
|
||||
// r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
|
||||
log.Info("Starting ciims proxy for : " + defaultURL)
|
||||
r.Run(listen)
|
||||
}
|
||||
|
||||
@@ -50,9 +52,13 @@ func sendMessage(c *gin.Context) {
|
||||
msg := internal.CreateSend(message.User, message.Pass,
|
||||
message.Priority, message.Event, message.Val, message.Msg)
|
||||
url := getURL(message.URL)
|
||||
resp := internal.Send(url, msg)
|
||||
resp, err := internal.Send(url, msg)
|
||||
errMsg := internal.GetErrMsg(resp)
|
||||
c.JSON(http.StatusOK, gin.H{"error": errMsg})
|
||||
if err == nil {
|
||||
c.JSON(http.StatusOK, gin.H{"error": errMsg})
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": errMsg})
|
||||
}
|
||||
} else {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
}
|
||||
@@ -69,8 +75,12 @@ func receiveMessage(c *gin.Context) {
|
||||
if err == nil {
|
||||
msg := internal.CreateReceive(message.User, message.Pass, message.Count)
|
||||
url := getURL(message.URL)
|
||||
resp := internal.Send(url, msg)
|
||||
c.JSON(http.StatusOK, gin.H{"msgs": internal.GetMsgs(resp)})
|
||||
resp, err := internal.Send(url, msg)
|
||||
if err == nil {
|
||||
c.JSON(http.StatusOK, gin.H{"msgs": internal.GetMsgs(resp)})
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
}
|
||||
} else {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
}
|
||||
|
||||
+6
-6
@@ -18,7 +18,7 @@ func post(url string, contentType string, msg string) (*http.Response, error) {
|
||||
return http.Post(url, contentType, bytes.NewBuffer([]byte(msg)))
|
||||
}
|
||||
|
||||
func postSim(url string, msg string) string {
|
||||
func postSim(url string, msg string) (string, error) {
|
||||
timeout := time.Duration(10 * time.Second)
|
||||
client := http.Client{
|
||||
Timeout: timeout,
|
||||
@@ -26,24 +26,24 @@ func postSim(url string, msg string) string {
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(msg))
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
return err.Error(), err
|
||||
}
|
||||
req.Header.Set("Content-Type", defaultContentType)
|
||||
req.Header.Set("User-Agent", defaultAgent)
|
||||
req.Header.Set("SOAPAction", "")
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
return err.Error(), err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
respBytes, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
return err.Error(), err
|
||||
}
|
||||
return string(respBytes)
|
||||
return string(respBytes), nil
|
||||
}
|
||||
|
||||
//Send message
|
||||
func Send(url string, message string) string {
|
||||
func Send(url string, message string) (string, error) {
|
||||
return postSim(url, message)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user