Send HTTP-Post Request in EventListener
Asked by Markus Gruber:

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();
```


Answers:
author: Markus Gruber
body: 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.

comments: author: Markus Gruber
body: 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.


Tags: authentication

post-function

rest

rest-api