GO语言用http包发送带json文本body的GET请求

 $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 main

import (
    "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)
}
 

相关推荐

  1. GO语言http发送json文本bodyGET请求

    2024-07-18 09:16:04       19 阅读
  2. go语言请求http接口示例 并解析json

    2024-07-18 09:16:04       37 阅读
  3. Curl- go net/http实现

    2024-07-18 09:16:04       53 阅读
  4. go进行httpget或postJson请求

    2024-07-18 09:16:04       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 09:16:04       49 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 09:16:04       53 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 09:16:04       42 阅读
  4. Python语言-面向对象

    2024-07-18 09:16:04       53 阅读

热门阅读

  1. Ubuntu 20 安装 uwsgi 失败解决办法

    2024-07-18 09:16:04       18 阅读
  2. 构建艺术:在Gradle中配置父子项目的关系

    2024-07-18 09:16:04       22 阅读
  3. (79)组合环路--->(03)组合环路代码示例一

    2024-07-18 09:16:04       19 阅读
  4. npm 设置镜像

    2024-07-18 09:16:04       17 阅读
  5. https 单向认证和双向认证

    2024-07-18 09:16:04       15 阅读
  6. 游戏中的敏感词算法初探

    2024-07-18 09:16:04       19 阅读
  7. opencv—常用函数学习_“干货“_11

    2024-07-18 09:16:04       17 阅读
  8. 云原生理解

    2024-07-18 09:16:04       20 阅读
  9. 银河麒麟部署 QtMqtt 解决 make 错误问题的教程

    2024-07-18 09:16:04       17 阅读
  10. 伪元素::before :: after的用法?

    2024-07-18 09:16:04       19 阅读