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
|
}
|