zhangzengfei
2023-08-15 851cb98ae2f9d1713a5443274ca14424b118018f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package nsqclient
 
import (
    "bytes"
    "fmt"
    "io/ioutil"
    "net/http"
)
 
const nsqWebApi = "http://121.31.232.83:9080/api/nsq/pub"
 
// http接口 http://121.31.232.83:9080/api/nsq/pub?topic=your_topic
func HttpPost(topic string, data []byte) bool {
    uri := nsqWebApi + "?topic=" + topic
 
    request, err := http.NewRequest(http.MethodPost, uri, bytes.NewReader(data))
    if err != nil {
        return false
    }
 
    request.Header.Set("Content-Type", "application/json;charset=UTF-8")
 
    response, err := http.DefaultClient.Do(request)
    if err != nil {
        fmt.Printf(err.Error())
        return false
    }
    defer response.Body.Close()
 
    body, _ := ioutil.ReadAll(response.Body)
 
    fmt.Println("response:", string(body))
 
    return true
}