1

I'm trying to create issue using jira rest api, here's my code:

 $new_issue = array(
            'fields' => array(
                'project' => array('key' => "KEY"),
                'summary' => $this->Summary,
                'description' => $this->Description,
                'issuetype' => array('name' => 'Bug')
            )
        );

        $body = json_encode($new_issue);

 self::$handle = curl_init();

 curl_setopt(self::$handle, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt(self::$handle, CURLOPT_POSTFIELDS, $body);
 curl_setopt_array(self::$handle, array(
    CURLOPT_URL => "jiraUrl//rest/api/2/issue/",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_HTTPHEADER => array("content-type:application/json"),
    CURLOPT_HEADER => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_ENCODING => ''
    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
    CURLOPT_USERPWD => $username . ':' . $password
      ));


     $response   = curl_exec(self::$handle);
            $error      = curl_error(self::$handle);

I'm getting this error: {"errorMessages":["No content to map to Object due to end of input"]} any suggestions?

3 Answers 3

1

Update for 2021, you can use this PHP Jira REST client: https://github.com/lesstif/php-jira-rest-client

<?php
require 'vendor/autoload.php';

use JiraRestApi\Issue\IssueService;
use JiraRestApi\Issue\IssueField;
use JiraRestApi\JiraException;

try {
    $issueField = new IssueField();

    $issueField->setProjectKey("TEST")
                ->setSummary("something's wrong")
                ->setAssigneeName("lesstif")
                ->setPriorityName("Critical")
                ->setIssueType("Bug")
                ->setDescription("Full description for issue")
                ->addVersion(["1.0.1", "1.0.3"])
                ->addComponents(['Component-1', 'Component-2'])
                // set issue security if you need.
                ->setSecurityId(10001 /* security scheme id */)
                ->setDueDate('2019-06-19')
            ;
    
    $issueService = new IssueService();

    $ret = $issueService->create($issueField);
    
    //If success, Returns a link to the created issue.
    var_dump($ret);
} catch (JiraRestApi\JiraException $e) {
    print("Error Occured! " . $e->getMessage());
}

And link to Jira docs

Sign up to request clarification or add additional context in comments.

Comments

0

Missing closing quotes here:

CURLOPT_URL => "jiraUrl//rest/api/2/issue/

and jiraUrl probably needs to be like $jiraUrl.

So, my take:

CURLOPT_URL => "$jiraUrl//rest/api/2/issue/",

1 Comment

thanks, but that's a typo that I made copying code to stackoverflow. Jira url is replaced with real jira url.
0

For anyone still wondering how to create an issue with JIRA REST API the below minimal code is working for me:

$url = "http://your.domain.here/rest/api/latest/issue/"
$username = "username";
$password = "password";
$txt = '{
    "fields": {
        "project": {
            "key": "KEY"
        },
        "summary": "SUMMARY",
        "description": "DESCRIPTION",
        "issuetype": {
            "name": "ISSUETYPE"
        }
    }
}';

// Create a new cURL resource
$ch = curl_init ();

// Set URL and other appropriate options
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $txt );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_USERPWD, $username . ":" . $password );

$headers = array ();
$headers [] = "Content-Type: application/json";
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );

// Grab URL and pass it to the browser
$result = curl_exec ( $ch );

echo $result;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.