2
votes

I got the following curl command to work on Linux:

curl -H "Content-Type:application/json" -H "Accept:application/json" -H "Authorization: Basic dGVsZXVuZzpuYWcweWEyMw==" -X PUT -d '{"requireJiraIssue": true, "requireMatchingAuthorEmail": "true"}' http://stash/rest/api/1.0/projects/TSD/repos/git-flow-release-test/settings/hooks/com.isroot.stash.plugin.yacc%3AyaccHook/enabled

However, when I tried to do this on PHP, the data is not being sent to the server properly, this is my set_opt commands:

$myURL = "http://stash/rest/api/1.0/projects/TSD/repos/git-flow-release-test/settings/hooks/com.isroot.stash.plugin.yacc:yaccHook/enabled";
$hookdata_yacc = array(
    'requireJiraIssue' => true,
    'requireMatchingAuthorEmail' => true
);
$data = json_encode($hookdata_yacc);
$headers = array(
    "Content-Type: application/json",
    "Accept: application/json",
    "Authorization: Basic dGVmyPasswordEyMw==",
    "Content-Length: " . strlen($data)
);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $myURL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_PUT, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

return (curl_exec($curl));

What did I miss?

1
What do you mean by "not sent properly"?Ali
@ArMonk You could upvote my answer as well if it solve your problemhindmost
upvoted, thanks for the help!ArMonk

1 Answers

4
votes

You use CURL options improperly. CURLOPT_PUT is intended for sending files, it is not suited for your case. You have to use CURLOPT_CUSTOMREQUEST option and set it to "PUT", not "POST".

So the code should look like this:

$myURL = "http://stash/rest/api/1.0/projects/TSD/repos/git-flow-release-test/settings/hooks/com.isroot.stash.plugin.yacc:yaccHook/enabled";
$hookdata_yacc = array(
    'requireJiraIssue' => true,
    'requireMatchingAuthorEmail' => true
);
$data = json_encode($hookdata_yacc);
$headers = array(
    "Content-Type: application/json",
    "Accept: application/json",
    "Authorization: Basic dGVmyPasswordEyMw==",
    "Content-Length: " . strlen($data)
);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $myURL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

return (curl_exec($curl));