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

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

Accepted Answer
1 vote

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
Community showcase
Posted Sep 18, 2018 in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

22,603 views 2 7
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you