钥匙斗篷PUT REST 415

时间:2018-10-30 09:24:54

标签: rest httpclient keycloak

当尝试使用HttpClient调用Keycloak的CALL REST API时,出现以下错误消息。代码如下:

ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

HttpServletRequest request = servletRequestAttributes.getRequest();
String token = request.getHeader("X-Auth-Token");
String url = authUrl + "/admin/realms/fast/users/"+userId+"/reset-password";

CloseableHttpClient closeableHttpClient = HttpClients.createDefault(); 
HttpPut httpPut = new HttpPut(url);
httpPut.addHeader("Authorization", token);
httpPut.addHeader("Content-Type ", "application/json");


List<NameValuePair>list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("type", "password"));
list.add(new BasicNameValuePair("value",userPassDO.getNewPassword()));
list.add(new BasicNameValuePair("temporary","true"));

UrlEncodedFormEntity entity;
try {
    entity = new UrlEncodedFormEntity(list,"UTF-8");
    httpPut.setEntity(entity);
    CloseableHttpResponse response = closeableHttpClient.execute(httpPut);
    HttpEntity et = response.getEntity();
    InputStream in = et.getContent();
    int stat=  response.getStatusLine().getStatusCode();
} catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

执行put请求后的响应显示错误

 "HttpResponseProxy{HTTP/1.1 415 Unsupported Media Type [Server: nginx/1.15.3, Date: Tue, 30 Oct 2018 09:13:03 GMT, Content-Length: 0, Connection: keep-alive] [Content-Length: 0,Chunked: false]"

有人有任何想法或遇到此问题吗?

0 个答案:

没有答案