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.
@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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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();
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks a lot. I dont know what is the problem but when I replace my code with your code, it worked. Thanks again.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.