Forums

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

how to add a label to jira issue using REST::Client with perl

mahesh tummala January 2, 2020

I am using REST::Client in Perl to consume jira rest api. I am struck with add label to an existing jira. I am using PUT method with uri: https:/<<hostname>>/rest/api/2/issue/ABC-123 with the body {"update": { "labels": [{"add":"Label_10" }] }}

It is working with postman but not with JIRA::Client. I am getting error code 405 with JIRA::Client. Our build scripts are written in perl.

 

Thanks in advance.

1 answer

0 votes
DPKJ
Community Champion
January 2, 2020

@mahesh tummala  Welcome to the community!!!

Have you added 'Content-Type' and 'Accept' header to your request with value as 'application/json'? And also can you share full response output and sample script for better support.

mahesh tummala January 2, 2020

Thanks for your quick help. Below is the code which I am using

 

my $content = "{'update': { 'labels': [{'add' : '$label' }] }}";
$content =~ s/'/"/g;
my $headers = {Content-Type => 'application/json', Accept => 'application/json', Authorization => 'Basic ' . encode_base64($jira_user . ':' . $jira_password)};
my $RestClient = REST::Client->new();
$RestClient->setHost("$jira_server_name");
print "Changing state for jira: $BRF_ID to $state wht the content:$content";
$RestClient->PUT("/rest/api/2/issue/$BRF_ID", $content,{ "Content-type" => 'application/json', Authorization => 'Basic ' . encode_base64($jira_user . ':' . $jira_password)});
my $responseCode = $RestClient->responseCode();
print "responseCode= $responseCode";
if($responseCode ne '204'){
my $error = $RestClient->responseContent();
print "Error in adding label:$label to jira:$issue_id";
print "error code: $responseCode, error message: $error\n";
exit;
}

 

Please note that I was able to do other operations using GET and POST(like adding a comment, changing jira status, linking an issue, etc.,). I am not able to set label to a jira.

DPKJ
Community Champion
January 2, 2020

I can't locate specific problem, but I think there is some issue in body content.

The code below work for me,

use REST::Client;
use MIME::Base64;

my $client = REST::Client->new();
my $username = '<USERNAME_OR_EMAIL_ON_CLOUD>';
my $password = '<PASSWORD_OR_API_TOKEN_ON_CLOUD>';
my $body = '{"update": {"labels": [{"add": "Label_10"}]}}';

$client->getUseragent()->ssl_opts(verify_hostname => 0);
$client->getUseragent()->ssl_opts(SSL_verify_mode => SSL_VERIFY_NONE);

$client->setHost("<JIRA_HOME_URL>");

$client->addHeader('Content-Type', 'application/json');
$client->addHeader('Accept', 'application/json');
$client->addHeader('Authorization', 'Basic ' . encode_base64($username . ':' . $password));




$client->PUT('/rest/api/2/issue/<ISSUE_ID>', $body);

print $client->responseCode();
mahesh tummala January 2, 2020

Thanks a lot. I dont know what is the problem but when I replace my code with your code, it worked. Thanks again.

binoykalathiparambil
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 19, 2022

I have noted that, if you are using "POST" method, we will get 405 Status code. When I changed to "PUT" my issue got resolved.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events