I'm trying to use Atlassian's REST api to add a version to a project in Jira. This works perfectly if I run it from git bash:
curl --request POST --url 'https://mysite.atlassian.net/rest/api/3/version' --header 'Authorization: Basic my_access_token' --header 'Accept: application/json' --header 'Content-Type: application/json' --data "{\"name\": \"19.16.0\",\"projectId\": 10011}"
But when I try to do the same thing via PHP, I get an error. I'm not super-familiar with using curl in PHP, so I'm hoping the issue is something really simple.
Here's my code:
$versionStr = $_POST['version'];
$project = $_POST['project'];
$url = "https://mysite.atlassian.net/rest/api/3/version";
$headers = array(
"Content-Type:application/json",
"Accept: application/json",
"Authorization: Basic my_access_token"
);
//The data you want to send via POST
$fields = array(
"name" => $versionStr,
"projectId" => $project
);
//url-ify the data for the POST
$fields_string = http_build_query($fields);
//open connection
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
// these error logs display the expected data
error_log("+++headers: " . print_r($headers, 1));
error_log("+++fields count: " . count($fields));;
error_log("+++fieldString: " . $fields_string);
//execute post
$result = curl_exec($ch);
error_log("+++Curl Result: " . print_r($result, 1));
curl_close($ch);
And here's the error, as returned in $result
:
{"errorMessages":["Unrecognized token 'name': was expecting 'null', 'true', 'false' or NaN\n at [Source: org.apache.catalina.connector.CoyoteInputStream@4dcb2cb6; line: 1, column: 6]"]}
Where am I going wrong? I'm assuming it's an issue with my formatting and not the endpoint, since as indicated, sending the same data through a raw curl call works.