我正在尝试使用RestTemplate
以制作PUT
. 出于某种原因,我无法重现PUT
我使用curl
它创建的内容,并且没有任何问题。
这是我的curl
调用成功并返回 200:
curl https://www.exampe.com \
-X PUT \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <bearer-token>" \
-v \
-d '{"json":"object"}'
这是试图复制此curl
调用的 Java 代码。在我的情况下,此代码将抛出HttpClientErrorException
status = 406:
boolean result = false;
String url = "https://www.example.com";
String json = "{\"json\":\"object\"}";
RestTemplate rest = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
headers.add("Authorization", String.format("Bearer %s", authToken));
HttpEntity<String> requestEntity = new HttpEntity<String>(json, headers);
ResponseEntity<String> responseEntity =
rest.exchange(url, HttpMethod.PUT, requestEntity, String.class);
HttpStatus status = responseEntity.getStatusCode();
这些请求有什么区别?如何将 Java 版本收敛到curl
版本?