Hello,
I am trying to create a page by sending a PUT request with the REST API but I can not find what must be page datas format.
This is my code :
function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
$res2 = CallAPI("PUT", "http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/XWiki/pages/test", "Hello World");
This is not creating the page, I do not know where I can chose the page location and everything else like title, …
Thanks for your help.
I have updated the previous code to this :
function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($curl, CURLOPT_PUT, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
$xmlFile = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<page xmlns="http://www.xwiki.org">
<title>Hello world</title>
<syntax>xwiki/2.1</syntax>
<content>TEST</content>
</page>';
$res2 = CallAPI("PUT", "http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/Main/pages/TEST", $xmlFile);
echo $res2;
But I now get the following error :
org.restlet.ext.jaxrs.internal.exceptions.ConvertRepresentationException: Could not convert the message body to a org.xwiki.rest.model.jaxb.Page at org.restlet.ext.jaxrs.internal.exceptions.ConvertRepresentationException.object(ConvertRepresentationException.java:49)
I do not understand where is the mistake.
I think that I have found the problem, I modified the PUT request with this line :
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");