gpt4 book ai didi

java - 尝试将新问题发布到 GitHub 存储库时出现错误 "400 Bad request"

转载 作者:行者123 更新时间:2023-11-30 01:56:22 25 4
gpt4 key购买 nike

我正在使用 spring 构建一个网络,该网络将允许用户查看存储库、他们的问题并根据需要添加新问题。当用户想要创建新问题时,就会出现该问题。我收到“错误 400 错误请求”,但我无法理解原因。

我尝试通过 URL 参数发送请求,但也不起作用。我还尝试使用 ObjectMapper 自动创建主体,但得到了相同的结果。所以我自己构建 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;
}

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

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

这一切都是我引用 GitHub api 文档完成的:https://developer.github.com/v3/issues/#create-an-issue

最佳答案

根据您在“创建问题”下提供的文档,“里程碑”的值应为整数。因此,查看您的请求,没有一个不是整数。我不确定您会在请求中提供什么 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”作为字符串。

关于java - 尝试将新问题发布到 GitHub 存储库时出现错误 "400 Bad request",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54369509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com