我正在尝试通过其API创建GitHub问题,但它返回状态422。当我使用硬编码值尝试该代码时,该代码有效,但是一旦我尝试使用来自stdin的输入,它就会得到以下内容响应
{
"message":"Validation Failed",
"errors":[{"resource":"Label",
"field":"name",
"code":"missing_field"
}],
"documentation_url":"https://developer.github.com/v3/issues/#create-an-issue"
}
请注意,我要使用封送处理,因为我正在从我正在研究的书中进行分配。
谢谢!
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)
}
答案 0 :(得分:0)
基于验证消息:
{
"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