Can't send plus('+') symbol as %2B to search by user key like someus+erkey

Max Rud September 11, 2017

I use java and atlassian-connect-spring-boot and I need to send request to 

/rest/api/2/user?key={user.key}

User key is usually a part of email someus+erkey@gmail.com. And emails allow using plus sign in them. The problem is that I can't use this code to send valid request.

return atlassianHostRestClients.authenticatedAsAddon().getForObject("/rest/api/2/user?key={0}", UserModel.class, "someus+erkey")

It seems that AtlassianConnectHttpRequestInterceptor decode someus%2Berkey and send it like someus+erkey. I'm getting org.springframework.web.client.HttpClientErrorException: 404 Not Found.

Is there any way to send request with encoded plus symbol with atlassian-connect-spring-boot?

1 answer

1 accepted

2 votes
Answer accepted
Max Rud September 12, 2017

To solve the problem, I used the following solution:

protected <T> ResponseEntity<T> sendCustomGet(String path, MultiValueMap<String, String> valueMap, Class<T> type) {
String baseUrl = getHostFromSecurityContext().map(AtlassianHost::getBaseUrl).orElse("");
MultiValueMap<String, String> encodedValueMap = new LinkedMultiValueMap<>();
valueMap.forEach((k, l) -> {
List<String> encodedValueList = new ArrayList<>();
l.forEach(v -> {
try {
encodedValueList.add(URLEncoder.encode(v, "UTF-8"));
} catch (UnsupportedEncodingException e) {
encodedValueList.add(v);
}
});
encodedValueMap.put(k, encodedValueList);
});

URI uri = UriComponentsBuilder.fromUriString(baseUrl).path(path)
.queryParams(encodedValueMap).build(true).toUri();

String token = atlassianHostRestClients.createJwt(HttpMethod.GET, uri);
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "JWT " + token);
RestTemplate template = new RestTemplate();

return template.exchange(uri, HttpMethod.GET, new HttpEntity<String>(headers), type);
}

 1. Get baseUrl from context

Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication())
.map(Authentication::getPrincipal)
.filter(AtlassianHostUser.class::isInstance)
.map(AtlassianHostUser.class::cast);


 2. Manually encode query parameters.

 3. Build URI with build(true).

 4. Generate JWT token atlassianHostRestClients.createJwt(HttpMethod.GET, uri) and add it to headers.

 5. Send request with template.exchange method.

Suggest an answer

Log in or Sign up to answer