Forums

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

Why am I getting 400 in GET request

shubham
Contributor
June 7, 2019

Below is the code snipped to get the tickets belonging to a specific project, final url is working when I directly open it in browser but I am getting status code 400 in my response can some one help.

 

I am running code like -

node test.js proj_Id severity

----------------------------------------------------------------

const request = require("request");
const jiraurl = "https://xyz.atlassian.net/rest/api/latest/search?jql=";
var auth = new Buffer("abc@cdf.com"+':'+"1234").toString('base64');
var getparams = {
url: jiraurl,
method: 'GET',
header: {
Authorization: 'Basic'+auth,
'Content-Type': 'application/json'
}
};

function getDetails(parameters, callback){
request(parameters, function(err, response, body){
if(!err){
var result="";
console.log(response);

if((parameters.method == "POST" && response.statusCode == 201) || (parameters.method == "GET" && response.statusCode == 200)){
console.log(response);
result = JSON.stringify(JSON.parse(response.body));
}
console.log("All good");
return callback(null, result);
} else {
console.log("Some error");
return callback(err, null);
}
});
};


function getTickets(projId, sev, callback){
getparams.url = jiraurl+"project="+projId+"%20and%20cf[12342]="+sev+"%20and%20assignee!=null";
console.log(getparams.url);
getDetails(getparams, function(err, data)
{
// data= JSON.parse(data);
console.log("Inside callback");
console.log(data);
if(!err)
console.log(data);
else
console.log("Error : "+err);
});
};


getTickets(process.argv[2], process.argv[3]);

 

1 answer

0 votes
Daniel Eads
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 10, 2019

Hey Shubham,

Looks like you're generating the basic auth header correctly (email:token -> base64 encoded) per our basic auth instructions. We have however recently starting returning 401's if you use a password rather than an API Token for requests to Atlassian Cloud.

You can have multiple API tokens per account (one for each script/application for example) and revoke them at any time without having to change your password. They're quite useful! Here are the instructions for generating a new API token:

  1. Log in to https://id.atlassian.com/manage/api-tokens.
  2. Click Create API token.
  3. From the dialog that appears, enter a memorable and concise Label for your token and click Create.
  4. Click Copy to clipboard, then paste the token to your script, or elsewhere to save:

Note:

  • For security reasons it isn't possible to view the token after closing the creation dialog; if necessary, create a new token.
  • You should store the token securely, just as for any password.

 

Replace the password in line 3 of your script with an API token and I think you'll be in business!

Cheers,
Daniel

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events