gpt4 book ai didi

json - github api 创建问题返回状态 422

转载 作者:数据小太阳 更新时间:2023-10-29 03:22:28 24 4
gpt4 key购买 nike

我正在尝试通过他们的 API 创建一个 GitHub 问题,但它返回状态 422。当我尝试使用硬编码值时代码有效,但是当我尝试使用来自标准输入的输入时,我得到以下信息回应

{
"message":"验证失败",
“错误”:[{“资源”:“标签”,
“字段”:“名称”,
“代码”:“缺少字段”
}],
“documentation_url”:“https://developer.github.com/v3/issues/#create-an-issue”
}

请注意,我想使用编码(marshal)处理,因为我正在根据我正在写的书做作业。

谢谢!

func main() {
var issue githubissues.IssueReq
issue = userInput()
//Hardcoded testvalues that work
var jsonStr = []byte(`{
"title": "Found a bug",
"body": "I'm having a problem with this.",
"assignees": [
"AssigneNR1"
],
"milestone": 1,
"labels": [
"bug"
]
}`)

issueMars, err := json.Marshal(issue)
if err != nil {
fmt.Println("Marshal Error", err)
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(issueMars))
req.Header.Set("X-Custom-Header", "myvalue")
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()

fmt.Println("Response Status:", resp.Status)
fmt.Println("Response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("Response body", string(body))
}

func userInput() githubissues.IssueReq {
var issue githubissues.IssueReq
reader := bufio.NewReader(os.Stdin)
fmt.Print("Issue Title:")
issue.Title, _ = reader.ReadString('\n')
fmt.Print("Body:")
issue.Body, _ = reader.ReadString('\n')
fmt.Print("Assignee:")
issue.Assignees = make([]string, 4)
assigne, _ := reader.ReadString('\n')
issue.Assignees = append(issue.Assignees, stripSpaces(assigne))
fmt.Print("Milestone:")
issue.Milestone, _ = reader.ReadString('\n')
issue.Milestone = strings.TrimSuffix(issue.Milestone, "\r\n")
fmt.Print("Label:")
issue.Labels = make([]string, 4)
label, _ := reader.ReadString('\n')
issue.Labels = append(issue.Labels, stripSpaces(label))
return issue

}

func stripSpaces(str string) string {
return strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, str)
}

最佳答案

基于验证消息:

{
"message": "Validation Failed",
"errors": [{
"resource": "Label",
"field": "name",
"code": "missing_field"
}],
"documentation_url": "https://developer.github.com/v3/issues/#create-an-issue"
}

您尝试附加到问题的标签似乎有问题。

尝试删除创建问题正文的这一部分:

 "labels": [
"bug"
]

或者创建一个名为“bug”的标签: https://developer.github.com/v3/issues/labels/#create-a-label

关于json - github api 创建问题返回状态 422,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51381660/

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