HTTP request server side
Asked by Jes?s Jorge Mu?oz:

Hello everyone,  

I am trying to make a HTTP request from server side, but I think I am not doing it in the proper way.  

I am using Node.js + ACE to make a Confluence connect add-on. I am trying to get the HTML code of a certain page in Confluence by using a macro.  

In index.js file, I have this code:  

```
app.get('/v1/cover-key', addon.authenticate(), function(req, res){
    var pageId = req.query['pageId'],
    pageVersion = req.query['pageVersion'],
    userKey = req.query['userKey'];
    var clientKey = req.context.clientKey;
 
    getHTTPClient (clientKey, userKey).get({
       url: '/rest/api/content/53150016?expand=body.view'
       },
       function(err, response, contents){

          if(err || (response.statusCode < 200 || response.statusCode > 299)) {
              console.log(err);
          }
       
       console.log(contents);
       /* Process content and render some view*/
       }
    );
});

function getHTTPClient (clientKey, userKey){
    return addon.httpClient({
       clientKey : clientKey,
       userKey : userKey,
       appKey : addon.key
    });
}
```

<br />

With this code, in server side console I get a lot of logging messages with the same content, as if the server was making continuous requests to the instance. In the other hand, in Confluence, I get this message:  

"There were errors rendering macro:

* An unknown error occurred."

<br />

Somehow, I figured out that I cannot concatenate query parameters directly to the url using this HTTP client, since if I change url by this one, everything runs fine:

```
/rest/api/content/53150016
```

<br />

Then, searching in [request client docs](https://github.com/request/request), I discovered that I must specify query parameters using "qs" object, so I changed my HTTP client code to this one:  

```
 getHTTPClient (clientKey, userKey).get({
          url: '/rest/api/content/53150016',
          qs : { expand : 'body.view'}
       },
       function(err, response, contents){

          if(err || (response.statusCode < 200 || response.statusCode > 299)) {
              console.log(err);
          }
       
       console.log(contents);
       /* Process content and render some view*/
       }
    );
```

<br />

But with this code, I get this error:

"HTTP Status 401 - Expecting claim 'qsh' to have value '82cff2bae10b32813530aa4ed7fa6ed70f9cf9dfff19f9b1916bcf350232e466' but instead it has the value 'dd7335e7bcae854cbb80f1f218605b98bbc1e37369aa288c4eff8e638fe485c8'"  

What is wrong here?  

Thanks in advance,  

Jesus


Answers:
author: Kombi, the best car ever
body: Hello, Jesus!

According to the [Confluence Cloud REST API Documentation:](https://developer.atlassian.com/cloud/confluence/rest/#api-content-id-get)
>
> #### 401 Unauthorized {#toc-hId-1400395872}
>
> Returned if the authentication credentials are incorrect or missing from the request.

You'll need to verify once again your authentication method, and make sure that you are following the guidelines from the documentation accordingly before you manage to fetch data from the */rest/api/{contentid}*endpoint. Refer to:

* [Confluence Cloud REST API Authentication](https://developer.atlassian.com/cloud/confluence/rest/#auth)

And double check your code, this should help you out.

Kind regards,

Marco A. Bomfim


Tags: app

atl-dev

cloud

connect