PUT Method not allowed, POST works, but I need to update an existing page not create one

I am running PHP script in tomcat to update page content in confluence REST API

$requestStorage = array(
"id" => $parentID,
"type" => "page",
"title" => $title,
"space" => array(
"key" => $workspaceKey
),
"body" => array(
"storage" => array(
"value" => "".$contentName."",
"representation" => "storage"
),
),
"version" => array(
"number"=> $version
),
);

$qbodyChild = json_encode($requestStorage);
$CURLOPT_URL= $GLOBALS['host'] ."rest/api/content/".$parentID ;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $CURLOPT_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $qbodyChild);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

$headers = array();
$headers[] = "Accept : application/json";
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Basic ".base64_encode($GLOBALS['username'].":".$GLOBALS['password']);
$headers[] = "Cache-Control: no-cache";
$headers[] = "Content-length:".strlen($requestStorage);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch); die;
}
curl_close ($ch);die;

 

I also tried adding  "ancestors" => array(
array(
"id" => $parentID
)
)

 

also work

curl -u admin:Abc@1234 -X PUT -H 'Content-Type: application/json' --data '{"id":"983786","type":"page","title":"title","body":{"storage":{"value":"<p>testNum</p>","representation":"storage"}},"version":{"number":2}}' http://localhost:8090/rest/api/content/983786

 

please help me ASAP

TIA

1 answer

0 votes

Hello Vidya and welcome to the Community!

What is the error/response you’re receiving when you attempt the PUT method on /rest/api/content/{contentId}?

There should be either a 400 or 404 thrown if the PUT method is not successful. From the response, we should be able to see why it is failing.

Common causes of content updates (PUT) failing are;

  • Due to the version already being used or version is incorrect
  • The account used does not have edit rights to the page. (Check your page restrictions)
  • Malformed JSON data

Once we have an response it should give us further context as to the failure.

Regards,
Stephen Sifers

Hello Stephen,

Thanks for your reply.

I am getting below response

HTTP Status 405 - Method Not Allowed

type Status report

message Method Not Allowed

description The specified HTTP method is not allowed for the requested resource.


Apache Tomcat/8.0.43

Working fine with curl request

curl -u admin:Abc@1234 -X PUT -H 'Content-Type: application/json' -d '{"id":"1378215","type":"page",
"title":"title","space":{"key":"NEOS"},"body":{"storage":{"value":"<p>This is the updated text for the new page</p>","representation":"storage"}},"version":{"number":5}}' http://localhost:8090/rest/api/content/1378215

also working in postman below is my request

http://localhost:8090/rest/api/content/1378215

headers:

Content-Type:appliction/json

Authorization:Basic YWRtaW46QWJjQDEyMzQ=


{
"id": "1378215",
"type": "page",
"title": "title",
"space": {
"key": "NEOS"
},
"body": {
"storage": {
"value": "<p>This is the updated text for the new page</p>",
"representation": "storage"
}
},
"version": {
"number": 5
}
}

 

Below is my  php code generated from postman, Which is working in XAMPP/WAMP server but not working with  tomcat server , all API's are working fine in tomcat server except this.


$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_PORT => "8090",
CURLOPT_URL => "http://localhost:8090/rest/api/content/1378215",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => "{\"id\":\"1378215\",\"type\":\"page\",\"title\":\"title\",\"space\":{\"key\":\"NEOS\"},\"body\":{\"storage\":{\"value\":\"<p>This is the updated text vid the new pageee</p>\",\"representation\":\"storage\"}},\"version\":{\"number\":5}}",
CURLOPT_HTTPHEADER => array(
"authorization: Basic YWRtaW46QWJjQDEyMzQ=",
"cache-control: no-cache",
"content-type: application/json",
),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

 

Please suggest me any solution , I am stick here....

TIA

Hello again Vidya,

Are you attempting to run this PHP file within Confluence or are you using a separate instance of apache tomcat? If you’re running this within Confluence, please ensure you review the following: How to use PHP inside Confluence pages.

I did confirm your PHP does work in XAMMP and confirmed the JSON payload is good. So there should not be an issue with the JSON payload, URL or authentication. Your error is stating the PUT method is not allowed, when in fact it is allowed as you and I have both verified. With this said, something is causing the method to be changed or not present during the API request.

In order to test further on this end, I need to understand how you’re accessing this PHP file and from which tomcat instance you’re using. From there we can figure out why the method is not allowed.

Regards,
Stephen Sifers

Hello Stephen,


http://www.studytrails.com/blog/php-on-a-java-app-server-apache-tomcat-using-quercus/


Please check above URL,&nbsp;
As per above URL I have created a blog folder in webapps folder .
Created script.php in it and accessing&nbsp; below URL in browser

http://localhost:8090/blog/script.php

Hello again Vidya,

I went through your document and I have the PHP file running as you specified.

When I tested the PHP file, I am noticing the CURL option for request is set to PUT however, it is being ignored and POST is being instead. This would explain why your getting the 405 response. This seems to possibly be an issue with how Quercus is rendering the PHP itself (to include the variable for $curl). This would also explain why it works in XAMMP but not within Quercus within Tomcat.

To see which method is being used, simply add the following after the curl variable:

echo $curl;

The should present an output similar to:

CurlResource[POST http://localhost:8090/rest/api/content/2588691]

The PHP format you have may not be compatible with Quercus and may need to be changed. I found a another method to using PUT requests in PHP: https://alex.tools/blog/sending-a-put-request-with-curl-in-php/

Regards,
Stephen Sifers

Hello Stephen,

I Implemented script as per above URL 

$url = 'http://localhost:8090/rest/api/content/1378215';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PUT, true);
$data = http_build_query([

"id" => 1378215,
"type" => "page",
"title" => 'title',
"space" => [
"key" => 'NEOS'
],
"body" => [
"storage" => [
"value" => "This is the updated text WE",
"representation" => "storage"
],
],
"version" =>[
"number"=> 13
]
]);

//var_dump($data);
$tmpfile = '/opt/atlassian/confluence/webapps/blog/WEB-INF/tmp/test.txt';
file_put_contents($tmpfile, $data);
chmod($tmpfile,777);
curl_setopt($ch, CURLOPT_INFILE, $tmpfile);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data));
$response = curl_exec($ch);
echo 'Status-Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE);
$headers = array();
$headers[] = "Content-Type: application/json;";
$headers[] = "Content-Length:".sizeof($tmpfile);
$headers[] = "Authorization: Basic ".base64_encode($GLOBALS['username'].":".$GLOBALS['password']);
$headers[] = "Cache-Control: no-cache";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
echo '<pre>';print_r(json_decode($response, true));echo'</pre>';
curl_close($ch);

 

 

getting 

HTTP Status 500 -

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Tuesday in Confluence

Confluence Admin Certification now $150 for Community Members

More and more people are building their careers with Atlassian, and we want you to be at the front of this wave! Important Dates Start the Certification Prep Course by 2 April 2019 Take your e...

213 views 2 10
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you