gpt4 book ai didi

java - 我收到错误 "org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request"

转载 作者:行者123 更新时间:2023-12-02 09:36:47 26 4
gpt4 key购买 nike

在 Spring Boot 中尝试从另一个 GET API 内部调用 POST API 时,我收到此错误“org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request”。以下是代码。我还在 get api 中获取多部分文件作为输入,并希望将该文件发送到此内部 post api

路径 tempFile = Files.createTempFile(null, null);

            Files.write(tempFile, newFile.getBytes());
File fileToSend = tempFile.toFile();

FileSystemResource fr = new FileSystemResource(fileToSend);

String baseUrl = "someURL which is correct";
URI uri = new URI(baseUrl);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
//headers.set("Content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
headers.set("Ocp-Apim-Subscription-Key","{key which i dont want to display}");

MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("newFile", fr);

HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);

RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity(uri, requestEntity, String.class);

return response;

最佳答案

在restTemplate.postForEntity方法中传递baseUrl字符串而不是uri并尝试

关于java - 我收到错误 "org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57442869/

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