json.Unmarshal 在进行 curl 调用时不填充结构

我正在尝试填充结构并进行一些进一步的操作,我在结构中的字段已导出,但当我进行 curl 时该值未填充。


package main


import (

    "encoding/json"

    "io/ioutil"

    "log"

    "net/http"


    "github.com/gorilla/mux"

)


type Y struct {

    Name      string   `json:"name"`

    EventType []string `json:"eventType"`

}


func main() {

    router := mux.NewRouter()

    router.HandleFunc("/config/export/eventtypes", eventTypes).Methods("GET")

    router.HandleFunc("/config/export/ia/outputs", createEventList).Methods("POST")

    http.ListenAndServe(":8000", router)

}


func eventTypes(w http.ResponseWriter, r *http.Request) {

    w.Header().Set("Content-Type", "application/json")


    var resp []map[string]string

    x := make(map[string]string)

    x["name"] = "HTTP"

    x["description"] = "http"

    resp = append(resp, x)

    x1 := make(map[string]string)

    x1["name"] = "KRB"

    x1["description"] = "http"

    resp = append(resp, x1)

    x2 := make(map[string]string)

    x2["name"] = "FileINFO"

    x2["description"] = "http"

    resp = append(resp, x2)


    json.NewEncoder(w).Encode(resp)

}


func createEventList(w http.ResponseWriter, r *http.Request) {

    var s Y

    x, _ := ioutil.ReadAll(r.Body)

    log.Printf("%v", string(x))

    json.Unmarshal([]byte(x), &s)

    log.Printf("%+v", s)


}

我正在测试这个,当我尝试使用 python 代码时curl -H "Content-Type: application/json" -X POST http://localhost:8000/config/export/ia/outputs -d'{"name":"test","eventType":"["http","dmtp"]"}',我的输出是{ []} 一样的


    myobj = {"name":"test","eventType":test}


y= requests.post("http://localhost:8000/config/export/ia/outputs",json=myobj)

印刷{Name:test EventType:[HTTP KRB FileINFO]}


侃侃无极
浏览 135回答 1
1回答

弑天下

删除 curl 请求中数组的引号
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go