Alaa-GI
Alaa-GI

Reputation: 410

Authentication to the Jira REST API using Sails JS

New to Sails, i 'am wondering how can i get access to my Jira account via Sails in order to create/edit/search some Issues with the Jira API REST.

I searched for this evrywhere, can someone help me please ?

Here is an exemple of what i want to do :

    module.exports = {

test: function(req, res)

  var https = require('https');
      var https = require('https'), options = {
        host : "jira.company.com",
        port : 80,
        path : "/rest/api/2/search?jql=issue=TASC-1",
        method : 'GET'};
  https.request(options, function(response) {
    var responseData = '';
    response.setEncoding('utf8');

response.on('data', function(chunk){
  responseData += chunk;
});

response.once('error', function(err){
  // Some error handling here, e.g.:
  res.serverError(err);
});

response.on('end', function(){
  try {


       // response available as `responseData` in `yourview`
    res.locals.requestData = JSON.parse(responseData);
  } catch (e) {
  sails.log.warn('Could not parse response from options.hostname: ' + e);
  }
  res.view('client');
}); }).end();}} 

But i got nothing in my View, i think it's normal because i did not authenticate.

Upvotes: 3

Views: 197

Answers (2)

Alaa-GI
Alaa-GI

Reputation: 410

it was an authentication problem, to resolve that,I simply add in the request header the login and password properties formatted as the official documentation explained it, so my code becomes:

module.exports = {

test: function(req, res)

  var https = require('https');
      var https = require('https'), options = {
        host : "jira.company.com",
        port : 443,
        path : "/rest/api/2/search?jql=issue=TASC-1",
        method : 'GET'
        headers: {
                 "Authorization": "Basic YWxbG9wMS4zp0bWFuzeThYS5l1TIqaXoxOTg5554Jh"
    }
           };
  https.request(options, function(response) {
    var responseData = '';
    response.setEncoding('utf8');

response.on('data', function(chunk){
  responseData += chunk;
});

response.once('error', function(err){
  // Some error handling here, e.g.:
  res.serverError(err);
});

response.on('end', function(){
  try {

       // response available as `responseData` in `yourview`
    res.locals.requestData = JSON.parse(responseData);
  } catch (e) {
  sails.log.warn('Could not parse response from options.hostname: ' + e);
  }
  res.view('client');
}); }).end();}} 

Upvotes: 1

Volodymyr Krupach
Volodymyr Krupach

Reputation: 918

You can use /rest/auth/1/session to authenticate and get cookies: https://docs.atlassian.com/jira/REST/latest/#d2e4234

or you can pass http basic auth headers with your /rest/api/2/search request.

Upvotes: 1

Related Questions