Go net/http获取body中json格式数据

Go net/http获取body中json格式数据

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
 
import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
)
 
type AutotaskRequest struct {
    RequestID string     `json:"requestid"`
    Clone     CloneModel `json:"clone"`
    Push      PushModel  `json:"push"`
}
 
type CloneModel struct {
    //TODO
    //"Method": string `json:"ceph"`
    RequestID   string `json:"requestid"`
    CallbackURL string `json:"callbackurl"`
}
 
type PushModel struct {
    RequestID   string `json:"requestiD"`
    CallbackURL string `json:"callbackuRL"`
    IP          string `json:"remoteip"`
    Port        int    `json:"remoteport"`
    User        string `json:"user"`
}
 
func test(w http.ResponseWriter, r *http.Request) {
    // r.ParseForm()
    defer fmt.Fprintf(w, "ok\n")
 
    fmt.Println("method:", r.Method)
    body, err := ioutil.ReadAll(r.Body)
    if err != nil {
        fmt.Printf("read body err, %v\n", err)
        return
    }
    println("json:", string(body))
 
    var a AutotaskRequest
    if err = json.Unmarshal(body, &a); err != nil {
        fmt.Printf("Unmarshal err, %v\n", err)
        return
    }
    fmt.Printf("%+v", a)
 
}
 
func main() {
    http.HandleFunc("/test", test)
    http.ListenAndServe(":8888", nil)
}

  客户端所传递参数如下:

复制代码
{
    "requestid": "xxxxx",
    "clone": {
        "method": "ceph",
        "callbackurl": "xxx",
        "remoteip": "192.168.2.1",
        "remoteport": 8080,
        "user": "xxx",
        "pass": "xxx",
        "path": "xxx",
        "filename": "xxx"
    },
    "optimize": {
        "callbackurl": "xxx",
        "filter": {
            "k1": true,
            "k2": false
        },
        "trim": true,
        "progressive": true,
        "quality": 100,
        "colorNum": 256,
        "gifOptLevel": 2,
        "svgo": true,
        "guetzli": false,
        "css_rewrite": false,
        "js_rewrite": false
    },
    "push": {
        "method": "ceph",
        "callbackurl": "xxx",
        "remoteip": "192.168.2.1",
        "remoteport": 8080,
        "user": "xxx",
        "pass": "xxx",
        "path": "xxx",
        "filename": "xxx"
    }
}
复制代码

json传递的参数中,服务端有的没有对应字段

 

posted @   impluse  阅读(27971)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示