How to call cloud rest api using express connect using JWT token

Hi Experts,

I am trying to get project list using cloud rest api in express connect app. I have JWT token how can use this to get project list.

Kindly help me with this.

 var options = {
      method: "GET",
      url: "https://xxxxxx.atlassian.net/rest/api/3/project",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
        // Authorization:
        //   "Basic xxxxx"
        //Authentication: "JWT " + req.body.token
        //Authorization: "Basic " + req.body.token
      }
      //body: bodyData
    };

    request(options, function(error, response, body) {
      if (error) throw new Error(error);
      console.log(
        "Response: " + response.statusCode + " " + response.statusMessage
      );
      console.log(body);
      res.json(body);
    });

I found a solution. In express, I used "httpClient " which is take care JWT authenticate automatically.

// working for project
     var httpClient = addon.httpClient(req);
    httpClient
        .get("rest/api/3/project", function(err, res, body) {
        console.log(body);
         res.json(body);
      });

Hi, I have the same issue,

Where do you run this ? inside the app.post() in the app.js file?

I tried using this but I receive this error:
Error: Http client options must specify clientKey

Hi @HadarEpstein,

Kindly refer to the below demo app. You can add this method in index.js file under the rountes folder.

Sample Code

 app.get('/addonData', addon.checkValidToken(), function(req, res) {
      var httpClient = addon.httpClient(req);

      httpClient.get({
            "headers": {
                "Content-Type": "application/json",
                "Accept": "application/json"
            },
            "url": "/rest/atlassian-connect/latest/addons/" + addon.key
        },
        function(err, response, body) {
            if (err) { 
              console.log(response.statusCode + ": " + err);
              res.send("Error: " + response.statusCode + ": " + err);
            }
            else {
                console.log(response.statusCode, body);
                res.send(body);
            }
        }
      );
    });

LINK
https://bitbucket.org/atlassianlabs/atlassian-connect-jira-example/src/master/routes/index.js