尝试将 post 新问题放入 GitHub 存储库时出现错误“400 错误请求”
I get error "400 Bad request" when trying to post a new issue into a GitHub repository
我正在使用 spring 构建一个网站,允许用户查看存储库、他们的问题并根据需要添加新问题。当用户想要创建一个新问题时,问题就会出现。我得到 "Error 400 Bad Request" 但我不明白为什么。
我尝试通过 URL 参数发送请求,但也没有成功。我还尝试使用 ObjectMapper 自动创建 body 但我得到了相同的结果。所以我正在自己构建 body 但是...再次出现相同的结果。
在评论 "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;
}
我希望此方法在存储库中创建新问题,然后重定向到存储库的问题列表。
我已经检查了 body,它对我来说似乎是正确的:
{
"title": "TestTitle",
"body": "TestBody",
"assignees": [],
"milestone": none,
"labels": []
}
我参考了 GitHub api 文档:https://developer.github.com/v3/issues/#create-an-issue
根据您在 'Create an issue' 下提供的文档,"milestone" 的值应该是一个整数。因此,根据您的要求,none 不是整数。我不确定您会在请求中提供什么 int,但我不相信 'none' 会起作用。
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": []
}
此外,查看 'List issues for a repository' 部分,他们似乎提到仅将 "none" 用作字符串。
正如 Brandon 和 NeplatnyUdaj 所说,这个问题可能与 "milestone line" 有关,因为它必须是整数。我放 "none" 是因为我不想添加任何里程碑,如果您想在问题创建后删除里程碑,它是使用的关键字。
感谢回答我的人,我发现您可以删除 "milestone line",因为在创建问题时无法告诉 API "there are no milestones" (尽管您可以根据文档在创建后删除所有里程碑)。
比你们都好!
我正在使用 spring 构建一个网站,允许用户查看存储库、他们的问题并根据需要添加新问题。当用户想要创建一个新问题时,问题就会出现。我得到 "Error 400 Bad Request" 但我不明白为什么。
我尝试通过 URL 参数发送请求,但也没有成功。我还尝试使用 ObjectMapper 自动创建 body 但我得到了相同的结果。所以我正在自己构建 body 但是...再次出现相同的结果。
在评论 "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;
}
我希望此方法在存储库中创建新问题,然后重定向到存储库的问题列表。 我已经检查了 body,它对我来说似乎是正确的:
{
"title": "TestTitle",
"body": "TestBody",
"assignees": [],
"milestone": none,
"labels": []
}
我参考了 GitHub api 文档:https://developer.github.com/v3/issues/#create-an-issue
根据您在 'Create an issue' 下提供的文档,"milestone" 的值应该是一个整数。因此,根据您的要求,none 不是整数。我不确定您会在请求中提供什么 int,但我不相信 'none' 会起作用。
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": []
}
此外,查看 'List issues for a repository' 部分,他们似乎提到仅将 "none" 用作字符串。
正如 Brandon 和 NeplatnyUdaj 所说,这个问题可能与 "milestone line" 有关,因为它必须是整数。我放 "none" 是因为我不想添加任何里程碑,如果您想在问题创建后删除里程碑,它是使用的关键字。
感谢回答我的人,我发现您可以删除 "milestone line",因为在创建问题时无法告诉 API "there are no milestones" (尽管您可以根据文档在创建后删除所有里程碑)。
比你们都好!