【发布时间】:2013-02-27 08:39:01
【问题描述】:
我正在尝试使用RestTemplate 来创建PUT。出于某种原因,我无法重现使用curl 创建的PUT,它可以顺利通过。
这是我的 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,状态= 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 版本?
【问题讨论】:
-
添加只是剪断似乎没有帮助。还有什么我应该研究的吗?
标签: java http curl put resttemplate