gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:33:26 25 4
gpt4 key购买 nike

我正在尝试填充结构并进行一些进一步的操作,我在结构中的字段已导出,但当我进行 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)

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

y= requests.post("http://localhost:8000/config/export/ia/outputs",json=myobj)
打印 {Name:test EventType:[HTTP KRB FileINFO]}

最佳答案

删除 curl 请求中数组的引号

关于go - json.Unmarshal 在进行 curl 调用时不填充结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63844849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com