Search code examples
javaspringapigithubhttp-status-code-400

I get error "400 Bad request" when trying to post a new issue into a GitHub repository


I'm building a web with spring that will allow the user to see the repositories, their issues and add new issues if they want. The problem appears when the user wants to create a new issue. I get "Error 400 Bad Request" and I can not underestand why.

I've tried to send the request through URL parameters but it didn't work either. I've also tried to automatically create the body with an ObjectMapper but I got the same result. So I'm building the body by myself but... same result again.

At the line with the comment "XXX" is where the software fails and in the web shows me the mentioned error.

@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;
}

I expected this method to create the new issue in the repository and then redirect to the repository's list of issues. I've checked the body and it seems to be correct to me:

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

I did it all consulting the GitHub api documentation: https://developer.github.com/v3/issues/#create-an-issue


Solution

  • As Brandon and NeplatnyUdaj said the issue was probably related with the "milestone line" because it was mandatory to be an integer. I put "none" because I did not want to add any milestone and it is the keyword used if you want to remove the milestones after the creation of the issue.

    Thanks to the people that answered me I figured out that you could remove the "milestone line" due there is no way to telling the API that "there are no milestones" in the creation of the issue (despite you can remove all the milestones after the creation according to the documentation).

    Than you all!