bash - How to urlencode data for curl command? - Stack Overflow
shell
noticeText="content=编译完成 ${JOB_NAME} ${release}"
curl --get --data-urlencode "$noticeText" http://10.67.78.65:9097/dingding_pack_notice_rev/message
noticeText="content=编译完成 ${JOB_NAME} ${release}"
curl --get --data-urlencode "$noticeText" http://10.67.78.65:9097/dingding_pack_notice_rev/message
如果有空格需要用字符串包一下
钉钉机器人:
go
package main
import (
"fmt"
"github.com/gin-gonic/gin" "io/ioutil" "log" "net/http" "strings"
)
func main() {
port := ":9097"
r := gin.New()
r.Use(gin.Logger())
r.Use(gin.Recovery())
gin.SetMode("debug")
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, "pong")
})
r.GET("/dingding_pack_notice_rev/message", func(c *gin.Context) {
content := c.Query("content")
go func() {
//dingDingNotice(content)
}()
c.JSON(http.StatusOK, "已接收content:"+content)
})
err := r.Run(port)
if err != nil {
fmt.Println("错误 %v", err)
}
}
// http://www.codebaoku.com/it-go/it-go-223689.html
// https://open.dingtalk.com/document/orgapp-server/message-types-and-data-format
func dingDingNotice(contentText string) {
webHook := `https://oapi.dingtalk.com/robot/send?access_token=xxxx`
content := `{"msgtype": "text",
"text": {"content": "【编译通知】:` + contentText + `"}
}` //创建一个请求
req, err := http.NewRequest("POST", webHook, strings.NewReader(content))
if err != nil {
// handle error
}
client := &http.Client{}
//设置请求头
req.Header.Set("Content-Type", "application/json; charset=utf-8")
//发送请求
resp, err := client.Do(req)
if err == nil {
// 解析Response
//returnMap, err := util.ParseResponse(resp) responseData, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
responseString := string(responseData)
fmt.Println(responseString)
} else {
fmt.Println(err)
}
//关闭请求
defer resp.Body.Close()
if err != nil {
// handle error
}
}
package main
import (
"fmt"
"github.com/gin-gonic/gin" "io/ioutil" "log" "net/http" "strings"
)
func main() {
port := ":9097"
r := gin.New()
r.Use(gin.Logger())
r.Use(gin.Recovery())
gin.SetMode("debug")
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, "pong")
})
r.GET("/dingding_pack_notice_rev/message", func(c *gin.Context) {
content := c.Query("content")
go func() {
//dingDingNotice(content)
}()
c.JSON(http.StatusOK, "已接收content:"+content)
})
err := r.Run(port)
if err != nil {
fmt.Println("错误 %v", err)
}
}
// http://www.codebaoku.com/it-go/it-go-223689.html
// https://open.dingtalk.com/document/orgapp-server/message-types-and-data-format
func dingDingNotice(contentText string) {
webHook := `https://oapi.dingtalk.com/robot/send?access_token=xxxx`
content := `{"msgtype": "text",
"text": {"content": "【编译通知】:` + contentText + `"}
}` //创建一个请求
req, err := http.NewRequest("POST", webHook, strings.NewReader(content))
if err != nil {
// handle error
}
client := &http.Client{}
//设置请求头
req.Header.Set("Content-Type", "application/json; charset=utf-8")
//发送请求
resp, err := client.Do(req)
if err == nil {
// 解析Response
//returnMap, err := util.ParseResponse(resp) responseData, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
responseString := string(responseData)
fmt.Println(responseString)
} else {
fmt.Println(err)
}
//关闭请求
defer resp.Body.Close()
if err != nil {
// handle error
}
}