ymajoros
ymajoros

Reputation: 2748

how to create an issue in jira via rest api?

Is it possible to create an issue in jira using REST api? I didn't find this in the documentation (no POST for issues), but I suspect it's possible.

A wget or curl example would be nice.

Upvotes: 42

Views: 78939

Answers (9)

Dumithu
Dumithu

Reputation: 51

This is C# code:

string postUrl = "https://netstarter.jira.com/rest/api/latest/issue";

var httpWebRequest = (HttpWebRequest)WebRequest.Create(postUrl);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("JIRAMMS:JIRAMMS"));

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    string json = @"{""fields"":{""project"":{""key"": ""JAPI""},""summary"": ""REST EXAMPLE"",""description"": ""Creating an issue via REST API 2"",""issuetype"": {""name"": ""Bug""}}}";

    streamWriter.Write(json);
    streamWriter.Flush();
    streamWriter.Close();

    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var result = streamReader.ReadToEnd();
    }
}

Upvotes: 5

Joe - Check out my books
Joe - Check out my books

Reputation: 16943

In order to create an issue, set a time estimate and assign it to yourself, use this:

  1. Generate an Atlassian token

  2. Generate & save a base64-encoded auth token:

    export b64token="$(echo "<your_email>:<generated_token>" | openssl base64)"

  3. Make a POST request:

curl -X POST \
  https://<your_jira_host>.atlassian.net/rest/api/2/issue/ \
     -H 'Accept: */*' \
     -H 'Authorization: Basic $b64token \
     -d '{
       "fields":{
         "project":{
           "key":"<your_project_key (*)>"
         },
         "issuetype":{
           "name":"Task"
         },
         "timetracking":{
           "remainingEstimate":"24h"
        },
         "assignee":{
           "name":"<your_name (**)>"
       },
       "summary":"Endpoint Development"
     }
   }'

Remarks:

(*) Usually a short, capitalized version of the project description such as: ...atlassian.net/projects/UP/.

(**) if you don't know your JIRA name, cURL GET with the same Authorization as above to https://<your_jira_host>.atlassian.net/rest/api/2/search?jql=project=<any_project_name> and look for issues.fields.assignee.name.

Upvotes: 1

JP Hochbaum
JP Hochbaum

Reputation: 647

Just stumbling on this and am having issues creating an issue via the REST API.

issue_dict = {
    'project': {'key': "<Key>"},
    'summary': 'New issue from jira-python',
    'description': 'Look into this one',
    'issuetype': {'name': 'Test'},
}
new_issue = jira.create_issue(issue_dict)

new_issue returns an already existing issue and doesn't create one.

Upvotes: 0

cyboashu
cyboashu

Reputation: 10443

To send the issue data with REST API we need to construct a valid JSON string comprising of issue details.

A basic example of JSON string:

 {“fields” : { “project” : { “key” : “@KEY@” } , “issuetype” : { “name” : “@IssueType@” } } }

Now, establish connection to JIRA and check for the user authentication. Once authentication is established, we POST the REST API + JSON string via XMLHTTP method. Process back the response and intimate user about the success or failure of the response.

So here JiraService being an XMLHTTP object, something like this will add an issue, where EncodeBase64 is a function which returns encrypted string.

Public Function addJIRAIssue() as String
With JiraService
    .Open "POST", <YOUR_JIRA_URL> & "/rest/api/2/issue/", False
    .setRequestHeader "Content-Type", "application/json"
    .setRequestHeader "Accept", "application/json"
    .setRequestHeader "Authorization", "Basic " & EncodeBase64
    .send YOUR_JSON_STRING

    If .Status <> 401 Then
        addJIRAIssue = .responseText
    Else
        addJIRAIssue = "Error: Invalid Credentials!"
    End If

End With

Set JiraService = Nothing
End Sub

You can check out a complete VBA example here

Upvotes: 1

Elye
Elye

Reputation: 60271

To answer the question more direct, i.e. using cURL.

To use cURL to access JIRA REST API in creating a case, use

curl -D- -u <username>:<password> -X POST --data-binary "@<filename>"  -H "Content-Type: application/json" http://<jira-host>/rest/api/2/issue/

And save this in your < Filename> (please edit the field per your Jira case) and save in the folder you call the cURL command above.

{
    "fields": {
       "project":
       { 
           "key": "<PROJECT_KEY>"
       },
       "summary": "REST EXAMPLE",
       "description": "Creating an issue via REST API",
       "issuetype": {
           "name": "Bug"
       }
   }
}

This should works. (note sometimes if it errors, possibly your content in the Filename is incorrect).

Upvotes: 3

mateuszb
mateuszb

Reputation: 1102

Now you can use REST + JSON to create issues.

To check which json fields you can set to create the issue use: https://jira.host.com/rest/api/2/issue/createmeta

For more information please see the JIRA rest documentation: https://docs.atlassian.com/jira/REST/6.2.4/

Upvotes: 2

msangel
msangel

Reputation: 10377

POST to this URL

https://<JIRA_HOST>/rest/api/2/issue/

This data:

{
"fields": {
   "project":
   { 
      "key": "<PROJECT_KEY>"
   },
   "summary": "REST EXAMPLE",
   "description": "Creating an issue via REST API",
   "issuetype": {
      "name": "Bug"
   }
  }
}

In received answer will be ID and key of your ISSUE:

{"id":"83336","key":"PROJECT_KEY-4","self":"https://<JIRA_HOST>/rest/api/2/issue/83336"}

Don't forget about authorization. I used HTTP-Basic one.

Upvotes: 42

Matt Quail
Matt Quail

Reputation: 6229

The REST API in JIRA 5.0 contains methods for creating tasks and subtasks.

(At time of writing, 5.0 is not yet released, although you can access 5.0-m4 from the EAP page. The doco for create-issue in 5.0-m4 is here).

Upvotes: 9

luuuis
luuuis

Reputation: 141

As of the latest released version (4.3.3) it is not possible to do using the REST API. You can create issues remotely using the JIRA SOAP API.

See this page for an example Java client.

Upvotes: 7

Related Questions