Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

how to fetch the jira data in json format using node js?

Pvs_Krishna March 3, 2020

I thought this was going to be simple, but it has proved surprisingly difficult.

What I want to achieve is to retrieve Jira data using nodejs (Visual Studio code).

Does anybody have any suggestions? Much appreciated at this stage...

1 answer

0 votes
Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 4, 2020

Hi @Pvs_Krishna 

Just before I opened your question, I noticed that 21 other people had looked at it, but no answers yet. I think this is because of what I'm about to ask you.

What have you tried so far? What goes wrong? Is there an error message? At what point in the process does something fail? Which API call are you trying?

I think if you put more detail into your question, with screenshots or possibly the code you're trying, you may get someone who can help. I wouldn't know where to start answering with the little info you've given.

Pvs_Krishna March 4, 2020

Hello @Warren Thanks for Your replay 

Actually I am trying to fetch all the JIRA data using postman (or) Through a Node js  Rest API. 

I am writing an API like. 

const os = require('os');
const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');

// const chilkat = require('jira-client');
router.use(bodyParser.json());
if (os.platform() == 'linux') {  
  
if (os.arch() == 'arm') {     
   
var chilkat = require('@chilkat_node6_arm');  
  } 
else if (os.arch() == 'x86') {    
    
var chilkat = require('@chilkat_node6_linux32');  
  } 
else {   
     
var chilkat = require('@chilkat_node6_linux64'); 
   }
else if (os.platform() == 'darwin') {  
  
var chilkat = require('@chilkat_node6_macosx');
}

function chilkatExample() {
    var rest = new chilkat.rest();  
  
var success;
    var URL = "https://forcitude.atlassian.net/rest/api/2/field";  
  
var bTls = true; 
   
var port = 443;  
  
var bAutoReconnect = true; 
   
success = rest.Connect("forcitude.atlassian.net",port,bTls,bAutoReconnect);  
  
if (success !== true) {   
     
console.log("ConnectFailReason: " + rest.ConnectFailReason);   
     
console.log(rest.LastErrorText);   
     
return;  
  }

    rest.SetAuthBasic("vamsi.paturu@forcitude.com","JIRA_API_TOKEN");
    rest.AddHeader("Accept","application/json");
    var sbResponseBody = new chilkat.StringBuilder(); 
   
success = rest.FullRequestNoBodySb("GET","/rest/api/2/field",sbResponseBody);  
  
if (success !== true) {    
    
console.log(rest.LastErrorText);   
     
return; 
   }

    var respStatusCode = rest.ResponseStatusCode;
    if (respStatusCode >= 400) {
        console.log("Response Status Code = " + respStatusCode);
        console.log("Response Header:");
        console.log(rest.ResponseHeader);
        console.log("Response Body:");
        console.log(sbResponseBody.GetAsString());
        return;
    }
    var jsonResponse = new chilkat.JsonArray();
    jsonResponse.LoadSb(sbResponseBody);
    var arrIdx = 0; 
   
var numArrayObjects = jsonResponse.Size; 
   
//  Iterate over the members of the array.
    while (arrIdx < numArrayObjects) { 
       
//  Make sure the array member at this index is a JSON object. 
       
if (jsonResponse.TypeAt(arrIdx== 3) { 
           
// jObj: JsonObject 
           
var jObj = jsonResponse.ObjectAt(arrIdx);
            //  ...
        }
        arrIdx = arrIdx+1;    }

}
chilkatExample();

1)But the node modules could n't installed  how to do that

AND

How to know the Issue token 

 

-Thanks 

vamsi

Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 4, 2020

Hi @Pvs_Krishna 

Okay, now I'll tell you that I don't actually know or use JS  :-(   but I do know C#, so can understand your code.

Unfortunately I wouldn't be able to help you with installing node modules, but I would recommend that you start with Postman (either download the app or use it online), because then you will know that your authentication and API call is working so that you can sort out the code.

The other point to note is that for authentication, the email / token combination needs to be 64 bit encoded, which I couldn't see in your code.

Warren
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 4, 2020

If you're able to run C# code, below is the code I use which works successfully (just replace the details for the variables Email, Token and BaseUrl. You would then call it by

public static string RunQuery(string query)
{
string Email = "your email address";
string Token = "your token";
string BaseUrl = @"https://yourdomain.atlassian.net" + query;
HttpWebRequest newRequest = WebRequest.Create(BaseUrl) as HttpWebRequest;
newRequest.ContentType = "application/json";
newRequest.Method = "GET";
newRequest.Proxy.Credentials = CredentialCache.DefaultCredentials;

string base64Credentials = GetEncodedCredentials(Email, Token);
newRequest.Headers.Add("Authorization", "Basic " + base64Credentials);

HttpWebResponse response = newRequest.GetResponse() as HttpWebResponse;

string result = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}

newRequest = null;
response = null;

return result;
}

private static string GetEncodedCredentials(string Email, string Token)
{
string mergedCredentials = string.Format("{0}:{1}", Email, Token);
byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
return Convert.ToBase64String(byteCredentials);
}

 

You would then call the API by

string result = RunQuery(@"/rest/api/2/field");

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events