2
votes

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?

2
Shouldn't your Authorization header value start with Basic and then the Base64 encoded username:password value.codingbadger

2 Answers

0
votes

When using powershell to communicate with any REST API, I highly recommend moving to powershell v3 so you can not only utilize Invoke-RestMethod but also ConvertTo[From]-Json cmdlets. That said, I haven't tried to get basic auth to work.

0
votes

Holy cow, this looks like an overly complex way to do this. I have tons of JIRA automation going with powershell, and I'm simply using cURL (because I'm too lazy to convert them all to Invoke-RESTblahblah) where I pass it my URL as a string and have a config file that does all the other stuff.

Is that of any benefit to you as an alternative approach?