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.

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

报告相同问题?

悬赏问题

  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码