带有 URL 编码数据的 Spring RestTemplate POST 请求
2022-09-01 08:02:04
我是Spring的新手,并试图使用RestTemplate进行休息请求。Java 代码应执行与以下 curl 命令相同的操作:
curl --data "name=feature&color=#5843AD" --header "PRIVATE-TOKEN: xyz" "https://someserver.com/api/v3/projects/1/labels"
但是服务器拒绝 RestTemplate 并带有400 Bad Request
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("PRIVATE-TOKEN", "xyz");
HttpEntity<String> entity = new HttpEntity<String>("name=feature&color=#5843AD", headers);
ResponseEntity<LabelCreationResponse> response = restTemplate.exchange("https://someserver.com/api/v3/projects/1/labels", HttpMethod.POST, entity, LabelCreationResponse.class);
有人能告诉我我做错了什么吗?