我需要向身份验证提供程序(Auth0)发送HTTP请求。我希望通过其电子邮件地址获得一个特定用户。
请求必须转到:
curl -H "Authorization: Bearer ..." https://.../users?q=email%3A%22mymail%40abc.de%22&search_engine=v2
我试过RestTemplate(Spring):
public void doSomething() {
...
Map<String, String> requestMap = new HashMap<>();
requestMap.put("q", "email%3A%22mymail%40abc.de%22");
requestMap.put("search_engine", "v2");
RestTemplate restTemplate = getRestTemplateForRequestMap();
HttpHeaders headers = getHeaders();
HttpEntity<Map<String, String>> request = new HttpEntity<>(requestMap, headers);
ResponseEntity<UserInfo[]> response = restTemplate.exchange(URI.create(baseApiUrl + "users"), HttpMethod.GET, request, UserInfo[].class);
...
}
private RestTemplate getRestTemplateForRequestMap() {
RestTemplate restTemplate = new RestTemplate();
List messageConverters = new ArrayList<>();
messageConverters.add(new MappingJackson2HttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);
return restTemplate;
}
看起来需要对q
参数的值进行编码(它们称之为“Lucene查询语法”)。我尝试了不同的变化。我甚至把它们“硬编码”编码。
然而,似乎q
参数不起作用,因为结果不仅包括具有指定电子邮件地址的用户,还包括所有用户。
标题很好(否则我根本就没有用户)。
答案 0 :(得分:0)
您可以在restTemplate.exchange
:
ResponseEntity<UserInfo[]> response = restTemplate.exchange(URI.create(baseApiUrl + "users"), HttpMethod.GET,
request, UserInfo[].class,requestMap);
答案 1 :(得分:0)
为我工作:
UriComponentsBuilder builder;
try {
String emailEncoded = URLEncoder.encode("email:" + email, "UTF-8");
builder = UriComponentsBuilder.fromHttpUrl(url)
.queryParam("include_fields", "true")
.queryParam("search_engine", "v3")
.queryParam("q", emailEncoded);
} catch (UnsupportedEncodingException e) {
.....
}
HttpEntity<?> httpEntity = HttpUtility.getHttpEntity(jwtToken);
ResponseEntity<String> response = restTemplate.exchange(builder.build().toUriString(), HttpMethod.GET, httpEntity, String.class);