首页 > 解决方案 > 尝试将新问题发布到 GitHub 存储库时出现错误“400 Bad request”

问题描述

我正在使用 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

标签: javaspringapigithubhttp-status-code-400

解决方案


根据您在“创建问题”下提供的文档,“里程碑”的值应该是整数。因此,查看您的请求,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”作为字符串。


推荐阅读