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

Max R I'm New Here Sep 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

1 vote
Max R I'm New Here Sep 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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,230 views 14 19
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot