Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,503,203
Community Members
 
Community Events
180
Community Groups

Send HTTP-Post Request in EventListener

Hi, I am pretty new to developing Confluence plugins and some help would be very much appreciated.

Here is the task: I have created a space blueprint plugin - let's call it spaceplugin. I also use another plugin, that has a rest-api - let's call it restplugin. When a space is created through my blueprint I want my spaceplugin to send a http-post to the rest-api of the restplugin. I got it to work in the javascript file, in the submit function:

AJS.safe.ajax({ 
dataType: 'json',
type: 'POST',
url: "/rest/restplugin/1.0/create",
data: ({spaceKey : state.spaceKey, name: "Test"}),
});

 The problem is that the space doesn't exist here and it doesn't work. What I tried next was create an EventListener that listened on the SpaceBlueprintCreateEvent. This works also fine, but I can't authenticate myself because I always get the following response:

?xml version="1.0" encoding="UTF-8" standalone="yes"?><status><status-code>401</status-code><message>Client must be authenticated to access this resource.</message></status>

I have tried various methods of sending the post in my listener, non seemed to work. (HttpClient, RequestFactory, addBasicAuthentication, ...). Also I think that I can only use the com.atlassian.sal.api that is installed on my confluence server and things like addTrustedTokenAuthentication() are not recognized there.

My question is now the following:

Can anyone provide me a solution for my problem? It can either be updating the version of sal.api on confluence, using the right classes to send the post, using the right token or any solution on how to get the javascript to work. Code for the right implementation is also very much appreciated. Maybe I am just making a small mistake, but I can't get it to work. This is really a big deal for me and any help is very much appreciated. Even though my "current" code doesn't work, I am posting it here - maybe it helps understanding the problem.

String restUrl = settingsManager.getGlobalSettings().getBaseUrl() + "/rest/restplugin/1.0/create";
Request request = requestFactory.createRequest(Request.MethodType.POST, restUrl);
String jsonString = "{\"spaceKey\":" + event.getSpace().getKey() + ",\"name\":\"Test\"}";
request.setRequestContentType("application/json");
request.setRequestBody(jsonString);
request.execute();

 

1 answer

1 accepted

0 votes
Answer accepted

Update:

I got it to work by adding the basic authentication myself:

 request.addHeader("Authorization", "Basic XXXXXXXXXXXXXXXXX");

 XXXXXX represents the encoded username and password. If I add this username and password in the following, it doesn't work:

 request.addBasicAuthentication("http://localhost:8090/", "XXXX", "XXXXXX");

What am I doing wrong? Is the hostname wrong? Also, if I check for the headers there is simply no header added after calling that method.

Update2:

 

Got it to work with Basic and also Trusted Authentication. The problem was indeed the hostname, I just put "localhost" without the port and the protocol and it worked.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events