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

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira Service Desk

Wy are we still using email for Service Desk workflows?

...attest to the experience of an urgent approval that gets lost in the boss’s inbox and requires that special “Please Approve” email or text message. In an age where we have distributed teams...

113 views 0 2
Read article

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