$curl http://192.168.1.99:8089/devices -X GET -H 'Content-Type: application/json' -H 'Accept: application/json' -d '{"nodeName": "192.168.1.111","containerId": "579"}'
{"status":1,"message":"ok","data":{"gpuId":5,"devUUID":"GPU-34dfc5f0-f402-900b-9e86-626a85d69686"}}
package mainimport (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type DeviceRequest struct {
NodeName string `json:"nodeName"`
ContainerId string `json:"containerId"`
}
type ApiResponse struct {
Status int `json:"status"`
Message string `json:"message"`
Data struct {
GpuId int `json:"gpuId"`
DevUUID string `json:"devUUID"`
} `json:"data"`
}
func main() {
// 创建请求数据
requestData := DeviceRequest{
NodeName: "192.168.1.111",
ContainerId: "d545d2da",
}
// 将请求数据序列化为JSON
jsonData, err := json.Marshal(requestData)
if err != nil {
fmt.Println("Error marshalling request data:", err)
return
}
// 创建HTTP请求
url := "http://192.168.1.99:8089/devices"
req, err := http.NewRequest("GET", url, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Println("Error creating request:", err)
return
}
// 设置请求头
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
// 发送请求
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
return
}
defer resp.Body.Close()
// 读取响应数据
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
return
}
// 解析响应数据
var apiResponse ApiResponse
err = json.Unmarshal(body, &apiResponse)
if err != nil {
fmt.Println("Error unmarshalling response body:", err)
return
}
// 打印响应数据
fmt.Printf("%+v\n", apiResponse)
}
GO语言用http包发送带json文本body的GET请求
平沙落雁子2024-07-22 13:56
相关推荐
DC_BLOG30 分钟前
IPv6(四)明朝百晓生36 分钟前
无线感知会议系列【3】【基于WiFi和4G/5G的非接触无线感知:挑战、理论和应用-1】城南云小白4 小时前
Linux网络服务只iptables防火墙工具羌俊恩4 小时前
视频服务器:GB28181网络视频协议Flying_Fish_roe4 小时前
linux-网络管理-网络配置运维小白。。5 小时前
Nginx 反向代理城南云小白5 小时前
web基础+http协议+httpd详细配置hellojackjiang20116 小时前
即时通讯框架MobileIMSDK的H5端开发快速入门有时间要学习6 小时前
Linux——应用层自定义协议与序列化Tony聊跨境6 小时前
什么是 SSL 代理?