duanlie4621 2019-01-02 14:55
浏览 319
已采纳

尝试创建问题时,来自GitHub API 422的意外错误响应

When posting an issue to GitHub API V3 I am getting an unexpected response. Namely 422 Unprocessable Entity. However the detail of the error is for the Search endpoint rather that the POST create endpoint. {"message":"Validation Failed","errors":[{"resource":"Search","field":"q","code":"missing"}],"documentation_url":"https://developer.github.com/v3/search"}

My instinct is that I have messed up the json but it is pretty simple and I can't see the issue. I have tried various solutions posted here and elsewhere but not found what I am doing wrong. This is a coding exercise rather than anything intended for production but driving me mildly insane.

  • Tested in Debug what the Request body is just before being posted. {"title":"Hello World","body":"dfsdfsdf "}
  • Tried removing the body as it is optional, same issue.
  • Tested in Debug that request is of type POST
  • Tested in Debug that authorization header is correct.
  • Removed authorization key and received 401 as expected.

The posting function:

func CreateIssue (issue *NewIssue) (*IssueDetailsResult, error){
issueJson, err := json.Marshal(issue)
if err != nil {
    log.Fatal(err)
    os.Exit(1)
}
req, err := http.NewRequest("POST", github.IssuesURL, bytes.NewBuffer(issueJson))
req.Header.Set("Authorization", "token "+os.Getenv("UPGITUSER"))
req.Header.Set( "Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
    log.Fatal(err)
    os.Exit(1)
}
if resp.StatusCode != http.StatusCreated {
    bodyBytes, _ := ioutil.ReadAll(resp.Body)
    body := string(bodyBytes)
    resp.Body.Close()
    return nil, fmt.Errorf("create issue failed:%s", resp.Status + "
text: " + body)
}
var result IssueDetailsResult
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
    resp.Body.Close()
    return nil, err
}
resp.Body.Close()
return &result, nil

}

Would expect 201 from GitHubAPI.

  • 写回答

1条回答 默认 最新

  • dsmvovm27249 2019-01-02 15:18
    关注

    The response is a strong indicator that the request is being sent to the wrong endpoint.

    You can use net/http/httputil's DumpRequestOut to inspect the requests you are about to send and to ensure that they are what you expect them to be.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向