我试图在没有请求正文的情况下创建一个restTemplate.postForObject(),但我收到了错误的请求。
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<>(headers);
CurrentAccount account = client.postForObject(
"https://api.dropboxapi.com/2/users/get_current_account", entity, CurrentAccount.class);
Run Code Online (Sandbox Code Playgroud)
错误
Caused by: org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request: [Error in call to API function "users/get_current_account": Bad HTTP "Content-Type" header: "application/x-www-form-urlencoded". Expecting one of "application/json", "application/json; charset=utf-8", "text/plain; charset=dropbox-cors-hack".]
Run Code Online (Sandbox Code Playgroud)
添加时
headers.setContentType(MediaType.APPLICATION_JSON);
Run Code Online (Sandbox Code Playgroud)
我收到一个错误
Caused by: org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request: [Error in call to API function "users/get_current_account": request body: could not decode input as JSON]
Run Code Online (Sandbox Code Playgroud)
邮递员请求无需正文即可工作