HI All,
does anyone have some example code for submitting a JIRA from a vb.net application (or C#)? I’ve been trying to build one but I’m not very familiar with JSON and I’m getting bad request errors.
If someone has a working example I’d really appreciate it.
regards
Kevin
Code I’m using for the build of the JSON string:
Dim objJira As New Jira()
objJira.Url = “http://jirasrv:8080/”
objJira.JsonString = "{""fields"": { " & vbCr & vbLf _
& """project"": { ""key"": ""XXX FSD"" }," & vbCr & vbLf _
& """summary"": ""Summary of the issue created using REST API…""," & vbCr & vbLf _
& """description"": ""Description of the issue""," & vbCr & vbLf _
& """issuetype"": { ""id"": ""3"" }," & vbCr & vbLf _
& """assignee"": { ""name"": ""XXXX"" }," & vbCr & vbLf _
& """reporter"": { ""name"": ""XXXX"" }," & vbCr & vbLf _
& """priority"": { ""id"": ""3"" }," & vbCr & vbLf _
& """customfield_10004"": ""Free text goes here. Type away!"" }" & vbCr & vbLf _
& "}"
objJira.UserName = "XXXXXX"
objJira.Password = "XXXXXX"
objJira.filePaths = New List(Of String)() From {"C:\xxxx.xls", "yyyyy.TXT"}
Try
objJira.AddJiraIssue()
Catch ex As Exception
MsgBox(ex)
End Try
Code I’m using to send the request:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Net
Imports System.IO
Imports System.Web
Class Jira
#Region “Variables, Properties and Enums”
Public Property UserName() As String
Get
Return m_UserName
End Get
Set(ByVal value As String)
m_UserName = value
End Set
End Property
Private m_UserName As String
Public Property Password() As String
Get
Return m_Password
End Get
Set(ByVal value As String)
m_Password = value
End Set
End Property
Private m_Password As String
Public Property Url() As String
Get
Return m_Url
End Get
Set(ByVal value As String)
m_Url = value
End Set
End Property
Private m_Url As String
Public Property JsonString() As String
Get
Return m_JsonString
End Get
Set(ByVal value As String)
m_JsonString = value
End Set
End Property
Private m_JsonString As String
Public Property filePaths() As IEnumerable(Of String)
Get
Return m_filePaths
End Get
Set(ByVal value As IEnumerable(Of String))
m_filePaths = value
End Set
End Property
Private m_filePaths As IEnumerable(Of String)
#End Region
#Region “Methods”
Public Sub AddJiraIssue()
Dim restUrl As String = [String].Format("{0}rest/api/2/issue/", Url)
Dim xxresponse As HttpWebResponse '= Nothing
Dim xxrequest As HttpWebRequest = TryCast(WebRequest.Create(restUrl), HttpWebRequest)
xxrequest.Method = "POST"
xxrequest.Accept = "application/json"
xxrequest.ContentType = "application/json"
xxrequest.Headers.Add("Authorization", "Basic " + Utility.GetEncodedCredentials(UserName, Password))
Dim data As Byte() = Encoding.UTF8.GetBytes(JsonString)
Using requestStream = xxrequest.GetRequestStream()
requestStream.Write(data, 0, data.Length)
requestStream.Close()
End Using
Try
Using response = TryCast(xxrequest.GetResponse(), HttpWebResponse)
If response.StatusCode <> HttpStatusCode.OK Then
Dim reader = New StreamReader(response.GetResponseStream())
Dim str As String = reader.ReadToEnd()
Console.WriteLine("The server returned '{0}'" & vbLf & "{1}", response.StatusCode, str)
Dim jss = New System.Web.Script.Serialization.JavaScriptSerializer()
Dim sData = jss.Deserialize(Of Dictionary(Of String, String))(str)
Dim issueKey As String = sData("key").ToString()
AddAttachments(issueKey, filePaths)
End If
End Using
Catch ex As Exception
XtraMessageBox.Show(ex.Message)
End Try
xxrequest.Abort()
End Sub
Public Sub AddAttachments(ByVal issueKey As String, ByVal filePaths As IEnumerable(Of String))
Dim restUrl As String = [String].Format("{0}rest/api/2/issue/{1}/attachments", Url, issueKey)
Dim filesToUpload = New List(Of FileInfo)()
For Each filePath As String In filePaths
If Not File.Exists(filePath) Then
Console.WriteLine("File '{0}' doesn't exist", filePath)
End If
Dim file__1 = New FileInfo(filePath)
filesToUpload.Add(file__1)
Next
If filesToUpload.Count <= 0 Then
Console.WriteLine("No file to Upload")
End If
PostFile(restUrl, filesToUpload)
End Sub
Private Sub PostFile(ByVal restUrl As String, ByVal filePaths As IEnumerable(Of FileInfo))
Dim zzresponse As HttpWebResponse = Nothing
Dim zzrequest As HttpWebRequest = Nothing
Dim boundary As [String] = [String].Format("----------{0:N}", Guid.NewGuid())
Dim stream = New MemoryStream()
Dim writer = New StreamWriter(stream)
For Each filePath As FileInfo In filePaths
Dim fs = New FileStream(filePath.FullName, FileMode.Open, FileAccess.Read)
Dim data = New Byte(fs.Length - 1) {}
fs.Read(data, 0, data.Length)
fs.Close()
writer.WriteLine("--{0}", boundary)
writer.WriteLine("Content-Disposition: form-data; name=""file""; filename=""{0}""", filePath.Name)
writer.WriteLine("Content-Type: application/octet-stream")
writer.WriteLine()
writer.Flush()
stream.Write(data, 0, data.Length)
writer.WriteLine()
Next
writer.WriteLine("--" + boundary + "--")
writer.Flush()
stream.Seek(0, SeekOrigin.Begin)
zzrequest = TryCast(WebRequest.Create(restUrl), HttpWebRequest)
zzrequest.Method = "POST"
zzrequest.ContentType = String.Format("multipart/form-data; boundary={0}", boundary)
zzrequest.Accept = "application/json"
zzrequest.Headers.Add("Authorization", "Basic " + Utility.GetEncodedCredentials(UserName, Password))
zzrequest.Headers.Add("X-Atlassian-Token", "nocheck")
zzrequest.ContentLength = stream.Length
Using requestStream As Stream = zzrequest.GetRequestStream()
stream.WriteTo(requestStream)
requestStream.Close()
End Using
Using response = TryCast(zzrequest.GetResponse(), HttpWebResponse)
If response.StatusCode <> HttpStatusCode.OK Then
Dim reader = New StreamReader(response.GetResponseStream())
Console.WriteLine("The server returned '{0}'" & vbLf & "{1}", response.StatusCode, reader.ReadToEnd())
End If
End Using
zzrequest.Abort()
End Sub
''' <summary>
''' Sends the request.
''' </summary>
''' <param name="uri">The URI.</param>
''' <param name="jsonDataBytes">The json data bytes.</param>
''' <param name="contentType">Type of the content.</param>
''' <param name="method">The method.</param>
''' <returns>System.String.</returns>
Private Function SendRequest(ByVal uri As Uri, ByVal jsonDataBytes As Byte()) As String
Dim req As WebRequest = WebRequest.Create(uri)
req.ContentType = "application/json"
req.Method = "POST"
req.ContentLength = jsonDataBytes.Length
Dim stream = req.GetRequestStream()
stream.Write(jsonDataBytes, 0, jsonDataBytes.Length)
stream.Close()
Dim response = req.GetResponse().GetResponseStream()
Dim reader As New StreamReader(response)
Dim res = reader.ReadToEnd()
reader.Close()
response.Close()
Return res
End Function
#End Region
End Class