gpt4 book ai didi

json - 如何从 HTTP 响应正文中读取错误

转载 作者:IT王子 更新时间:2023-10-29 02:07:08 24 4
gpt4 key购买 nike

这就是错误的样子,当我尝试 fmt.Println(err) 时,我只需要 "Body"

控制台

Expected HTTP response code [200] when accessing [POST http://controller:8774/v2.1/os-keypairs], but got 409 instead
{"conflictingRequest": {"message": "Key pair 'Darkhaa test hi' already exists.", "code": 409}}

Controller

createKeyPair, err := compute.CreateKeypair(raw["keyPairName"].(string))

if err != nil {
fmt.Println(err)
lists["retType"] = -1
lists["retDesc"] = err
} else {
lists["retType"] = 0
lists["retDesc"] = ""
lists["retData"] = createKeyPair
}

i.Data["json"] = lists

最佳答案

type ErrorStruct struct {
ConflictingRequest struct {
Message string `json:"message"`
Code int `json:"code"`
} `json:"conflictingRequest"`
}

go func() {
_, err := compute.CreateKeypair(raw["keyPairName"].(string))
if err != nil {
re := regexp.MustCompile("\\{(.*?)\\}")
match := re.FindStringSubmatch(err.Error())
data := ErrorStruct{}
json.Unmarshal([]byte(match[1]), data)
log.Printf("Message: %s", data.Message)
}
}

关于json - 如何从 HTTP 响应正文中读取错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55372166/

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