Create JIRA Issue via REST API and Powershell

I have been trying to interact with the JIRA REST API (version = 5.2-m06-4)

I have code that looks as follows:

function ConvertTo-Base64($string) {
   $bytes  = [System.Text.Encoding]::UTF8.GetBytes($string);
   $encoded = [System.Convert]::ToBase64String($bytes); 

   return $encoded;
}
$reqBody = [System.Text.Encoding]::UTF8.GetBytes($data)
        
        try{
                
                $authenticationDetails = ConvertTo-Base64 '$username:$password'
                Write-Host('Opening a connection to {0}' -f $url)
                
                $req = [System.Net.WebRequest]::Create($Url)
                $req.Headers.Add("AUTHORIZATION", $headerValue);
                $req.Method = "POST"
                $req.ContentType = "application/json"          
                $req.Timeout = $Timeout.TotalMilliseconds
                
                $req.Credentials = New-Object System.Net.NetworkCredential -ArgumentList $username, $password 
                $req.PreAuthenticate = $true

                $req.ContentLength = $reqBody.Length   
                $reqStream = $req.GetRequestStream()            
                $reqStream.Write($reqBody, 0, $reqBody.Length)
                $reqStream.Close()

                Write-Host($data)
                
                $resp = $req.GetResponse()
                Write-Verbose $resp
                Write-Output $resp.StatusCode
        }
        catch [System.Net.WebException]{
                if ($_.Exception -ne $null -and $_.Exception.Response -ne $null) {
                        $errorResult = $_.Exception.Response.GetResponseStream()
                        $errorText = (New-Object System.IO.StreamReader($errorResult)).ReadToEnd()
                        Write-Warning "The remote server response: $errorText"
                        Write-Output $_.Exception.Response.StatusCode
                        Write-Host $_
                } else {
                        throw $_
                }
        }

The code is passed JSON as follows:

$jsonString = '{"fields": {"project":{ "key": "CCB"},"components": "' + $COMPONENT + '"},"customfield_11502" : "' + $DEPLOYMENT_DATE + '","Summary": "' + $description + '","issuetype": {"name": "Configuration Change"}}}'

The issue I am having is that when I try and POST data in this way, I am told that summary, customfield_11502 and components are not part of the Screen. When i try and remove these properties of the JSON, I am told that I am not authorized to post an issue.

What is the correct error message here? Is it that I am not authorised and if so is the fields missing from the screen not really an issue?

When passing a username and password over the REST API, I am Base64 encoding it and also passing it in the Headers. IS there anything obvious I am missing?

Thanks

Paul

3 answers

Hi Paul,

Regarding the first error "customfield_11502 and components", you need to make sure those fields have been added to the default screen of the issue creation.

Regarding the second error "authorized to post an issue.", it sounds that you don't have enough permission to create an issue in a specific project, would you mind to check by creating an issue with the normal UI first to make sure everthing is going fine?

Also that would be great if you can post here the HTTP Request headers and the Response headers.

Cheers,

Omar

The default screen for creating the issue has only got the 5 fields I am trying to pass. I am an administrator of the system and I am passing that username and password to test. There should be no restrictions.

the issue I am more worried about, is why is the first error telling me about the screens? Im passing the same username and password to both systems

Paul

Paul,

Can you please raise a support ticket at https://support.atlassian.com regarding this issue, we'll look at further there.

Cheers,

Omar

Hi Stack,

AFAIK you can't perform administrative tasks via REST API (such as atribute custom fields to screens). It's what you're trying to do?

Best regards,
Lucas Timm

Im not trying to do any administrative tasks at all. Im trying to create a new Issue via the REST API

you can add summary, customfield_11502 and components on create issue screen and try again

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Amir Kazemi
Posted 4 hours ago in Jira

We want to know what Jira Service Desk apps you're using!

Hi Community! My name is Amir and I’m on the Jira Service Desk product marketing team at Atlassian. Our team would love to understand how you’re leveraging our ecosystem for Jira Service Desk. Wha...

21 views 0 4
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you