How to make an API call using JavaScript?

John Nguyen February 23, 2018

Hi All,

  I'm a village idiot on API things so please bear with my stupid questions.

 Here is my problem to solve: I have a JIra project and want to extract some data from my issues and inject them to an html page.

I looked up JIRA API docs and was not sure of how to use them properly (syntax wise).

I also tried out some APIs via CLI such as:

curl -D- -u nguyen7744@yahoo.com:<my password>GET -H "Content-Type: application/json" https://automationexpert.atlassian.net/rest/api/latest/myself

It works beautifully :-).

Now I want to create a Javascript program that will make similar API call to my Jira.

Here is how my sample JavaScript code (learned form youtube) looks like:

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var ourRequest = new XMLHttpRequest();
ourRequest.open('GET', 'https://learnwebcode.github.io/json-example/animals-1.json');
ourRequest.onload = function () {
"use strict";
console.log(ourRequest.responseText);

};

Question:

How can I replace the string 'https://learnwebcode.github.io/json-example/animals-1.json'  with the string 'curl -D- -u nguyen7744@yahoo.com:<my password>GET -H "Content-Type: application/json" https://automationexpert.atlassian.net/rest/api/latest/myself'

In other words, I'd like to extract the "myself" data from my Jira with basic authentication.  

What  do I need to do?  Thanks!

 

 

1 answer

3 votes
John Nguyen February 28, 2018

I found the answer buried deep in the Atlassian do on using the "cookie" authentication

https://developer.atlassian.com/cloud/jira/platform/jira-rest-api-cookie-based-authentication/

 

Not quite of what I was looking for but it is good enough for my excercise.

J.N

HH
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.
July 4, 2019

Hi @John Nguyen Did you get this working? I'm struggling to get it to work. If you have any sample code you could share that would be fantastic!

John Nguyen July 4, 2019

Hollie

  I did have the code and examples but I do not remember where they are at this moment.  This is just a fun exercise for me because I was curious to see how I can use JS to make JIra API calls.

My recommendation - however  - is not to use this simple approach.  User OAuth  to make API calls.  It is much more secure than the approach described in the link above.  

  Here is the link on how to implement OAuth ( in Java) and make calls to Jira APIs

  https://developer.atlassian.com/server/jira/platform/oauth/

  One note of caution though.  Current Jira APIs can only support OAuth version 1.0a which is not as secure as version 2.0.  But Jira is behind in this area... and I'm not sure what the reason is?  It is a security risk.

  And keep in mind that the code in the link above is for server side.  You need to write your own JavaScript to call your API ( see highlight of my original code).

  I do understand this is quite complex  process and you need both the sample codes- java code for OAuth for the server side and JS code for client side, but I'm in the middle of studying for a big exam so I'm not able to help you at this moment.  Not a promise, but I'll try my best to build a sample project for you and will post the code on my GitHub so you can download them.

  For now, you can try to learn the rope via the links I sent you.  Have fun and good luck!

  John Nguyen

Like Lewis Smith likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events