rts-sim-testing-service/dynamics/http.go

40 lines
875 B
Go

package dynamics
import (
"bytes"
"encoding/json"
"fmt"
"go.uber.org/zap"
"joylink.club/bj-rtsts-server/config"
"net/http"
)
func SendInitTrainReq(info *InitTrainInfo) (int, *[]byte, error) {
defer func() {
if r := recover(); r != nil {
zap.S().Error("发送列车初始化请求失败", r)
}
}()
ip := config.Config.Dynamics.Ip
var port string
if config.Config.Dynamics.HttpPort != 0 {
port = fmt.Sprintf(":%d", config.Config.Dynamics.HttpPort)
}
uri := "/api/aerodynamics/init/train"
url := "http://" + ip + port + uri
data, _ := json.Marshal(info)
resp, err := http.Post(url, "application/json", bytes.NewBuffer(data))
if err != nil {
s := err.Error()
println(s)
return 0, nil, err
}
var buf []byte
_, err = resp.Body.Read(buf)
if err != nil {
return resp.StatusCode, nil, err
}
return resp.StatusCode, &buf, resp.Body.Close()
}