【问题标题】:I get error "400 Bad request" when trying to post a new issue into a GitHub repository尝试将新问题发布到 GitHub 存储库时出现错误“400 Bad request”
【发布时间】:2019-06-19 12:55:04
【问题描述】:

我正在使用 spring 构建一个 web,它将允许用户查看存储库、他们的问题并在需要时添加新问题。当用户想要创建新问题时,就会出现问题。我收到“错误 400 错误请求”,我不明白为什么。

我尝试通过 URL 参数发送请求,但也没有成功。我也尝试使用 ObjectMapper 自动创建主体,但得到了相同的结果。所以我自己构建了身体,但是......再次得到同样的结果。

在带有注释“XXX”的那一行是软件失败的地方,并且在网络上显示了提到的错误。

@PostMapping("newIssue/{user}/{repo}/{fullName}")
public String registerUser(@PathVariable String user, @PathVariable String repo, @PathVariable String fullName, @Valid NewIssue newissue, Errors errors, Model model, OAuth2AuthenticationToken authentication) throws JsonProcessingException {

    //To debug
    System.out.println("### Registering issue");

    //Check errors
    List<String> errorsStrings = new ArrayList<>();
    errors.getAllErrors().forEach(e->errorsStrings.add(e.getDefaultMessage()));
    model.addAttribute("errors", errorsStrings);
    model.addAttribute("newissue", newissue);
    if(errors.hasErrors()) {
        //To debug
        System.out.println("### HAS ERRORS");
        for (String err: errorsStrings )
            System.out.println("    " + err);
        //If has errors show again the page
        return "newIssue";
    }

    //To debug
    System.out.println("### Does not have ERRORS");

    //Create the client variable
    OAuth2AuthorizedClient client = authorizedClientService.loadAuthorizedClient( authentication.getAuthorizedClientRegistrationId(), authentication.getName() );

    //Construct the necessary headers
    HttpHeaders headers = new HttpHeaders();
    headers.add(HttpHeaders.AUTHORIZATION, "token " + client.getAccessToken().getTokenValue());
    headers.add(HttpHeaders.ACCEPT, "application/vnd.github.v3+json");

    //Construct the html petition's body
    ObjectMapper mapper = new ObjectMapper();
    //String body = mapper.writeValueAsString(newissue);
    String body =
            "{\n" +
            "  \"title\": \"" + newissue.getTitle() + "\",\n" +
            "  \"body\": \"" + newissue.getBody() + "\",\n" +
            "  \"assignees\": [],\n" +
            "  \"milestone\": none,\n" +
            "  \"labels\": []\n" +
            "}"
    ;

    //Merge the header and the body
    HttpEntity<String> request = new HttpEntity<String>(body, headers);

    //To debug
    System.out.println("### Going to send post: ");
    System.out.println(body);

    //Send the issue to the api
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<String> response = restTemplate.exchange("https://api.github.com/repos/" + user + "/" + repo + "/issues", HttpMethod.POST, request, String.class); //XXX

    //To debug
    System.out.println("### Post sent");

    //To debug
    System.out.println("### RESPONSE: " + response);

    //Go to the repos' issues webpage
    return "redirect:issues/"+user+"/"+repo+"/"+fullName;
}

我希望此方法在存储库中创建新问题,然后重定向到存储库的问题列表。 我检查了身体,对我来说似乎是正确的:

{
  "title": "TestTitle",
  "body": "TestBody",
  "assignees": [],
  "milestone": none,
  "labels": []
}

这一切我都参考了 GitHub api 文档:https://developer.github.com/v3/issues/#create-an-issue

【问题讨论】:

  • 根据文档,milestone 应该是一个整数。
  • 如果我将“none”更改为任何整数,则响应将变为“422 Unprocessable Entity”。我想这是由于存储库中缺少任何里程碑。 (我试过 1、0 和 -1)

标签: java spring api github http-status-code-400


【解决方案1】:

根据您在“创建问题”下提供的文档,“里程碑”的值应该是整数。因此,查看您的请求,none 不是整数。我不确定你会在请求中提供什么 int,但我不相信“无”会起作用。

   String body =
        "{\n" +
        "  \"title\": \"" + newissue.getTitle() + "\",\n" +
        "  \"body\": \"" + newissue.getBody() + "\",\n" +
        "  \"assignees\": [],\n" +
        "  \"milestone\": 0,\n" +
        "  \"labels\": []\n" +
        "}"
   ;

这将创建以下主体:

{
    "title": "TestTitle",
    "body": "TestBody",
    "assignees": [],
    "milestone": 0,
    "labels": []
}

此外,查看“列出存储库的问题”部分,他们似乎提到仅使用“none”作为字符串。

【讨论】:

  • 感谢您让我思考。正如我之前在另一条评论中提到的,如果我将“none”更改为任何整数,则响应将变为“422 Unprocessable Entity”。我想这是由于存储库中缺少任何里程碑。将“none”更改为字符串(添加引号)给了我与没有相同的结果。但是,我可以完全删除里程碑线,然后一切正常!我认为这是强制性的。谢谢!
【解决方案2】:

正如 Brandon 和 NeplatnyUdaj 所说,这个问题可能与“里程碑线”有关,因为它必须是整数。我输入“none”是因为我不想添加任何里程碑,如果您想在创建问题后删除里程碑,它就是使用的关键字。

感谢回答我的人,我发现您可以删除“里程碑行”,因为无法告诉 API 在创建问题时“没有里程碑”(尽管您可以删除所有根据文档创建后的里程碑)。

比你们所有人都好!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 2013-02-14
    • 2022-12-14
    • 1970-01-01
    • 2017-11-06
    • 2015-06-17
    相关资源
    最近更新 更多