Формат хранения как тело в Confluence Rest API (создание страницы)

Я занимаюсь разработкой импортера страниц с использованием PHP и столкнулся с проблемой создания страниц с использованием формата хранения. В основном я получаю неправильно сформированную ошибку xhtml:

HTTP/1.1 400 Bad Request
Date: Thu, 14 Jan 2016 18:13:48 GMT
Server: Apache-Coyote/1.1
X-ASEN: SEN-6458441
X-Seraph-LoginReason: OK
X-AUSERNAME: XXX
Cache-Control: no-cache, must-revalidate
Expires: Thu, 01 Jan 1970 00:00:00 GMT
X-Content-Type-Options: nosniff
Content-Type: application/json
Connection: close
Transfer-Encoding: chunked

{
"statusCode": 400,
"data": {
"authorized": false,
"valid": true,
"errors": []
},
"message": "Error parsing xhtml"}

Нужно было создать phpDocumentor как исходную документацию, поэтому я создал страницу вручную, со всеми макросами, необходимыми для правильного отображения документации класса. Проблема в том, что когда я использую опцию «Просмотр формата хранения», полученный XML-код недействителен:

<ac:layout><ac:layout-section ac:type="single"><ac:layout-cell>
<p>Test</p><ac:structured-macro ac:macro-id="3e74101c-857f-4c47-8618-a27c627eea2c" ac:name="info" ac:schema-version="1"><ac:parameter ac:name="title">Fully qualified class name</ac:parameter><ac:rich-text-body>\Zend\Authentication\Adapter\Digest</ac:rich-text-body></ac:structured-macro>
<p><a href="https://github.com/zendframework/zend-authentication/blob/master/src/Adapter/Digest.php">Browse source code</a></p></ac:layout-cell></ac:layout-section><ac:layout-section ac:type="two_left_sidebar"><ac:layout-cell>
<h2>Class contents</h2>
<p><ac:structured-macro ac:macro-id="726aa6fa-6e48-4005-95ad-0fb28aae8162" ac:name="toc" ac:schema-version="1"><ac:parameter ac:name="maxLevel">4</ac:parameter><ac:parameter ac:name="minLevel">3</ac:parameter></ac:structured-macro></p>
<p>&nbsp;</p></ac:layout-cell><ac:layout-cell>
<h2>Description</h2>
<p><ac:structured-macro ac:macro-id="c7feab99-a70b-4ab8-bbac-846ed981bed0" ac:name="excerpt" ac:schema-version="1"><ac:parameter ac:name="atlassian-macro-output-type">INLINE</ac:parameter><ac:rich-text-body>
<p>Digest adapter for Zend Authentication mechanism</p></ac:rich-text-body></ac:structured-macro>&nbsp;</p>
<h2>Class details</h2>
<table style="letter-spacing: 0.1px;">
<tbody>
<tr>
<th>Package</th>
<td>\Zend\Authentication\Adapter</td></tr>
<tr>
<th>Inherited from</th>
<td><span style="color: rgb(68,68,68);">\Zend\Authentication\Adapter\AbstractAdapter</span></td></tr></tbody></table>
<h3>Methods</h3>
<hr /><ac:structured-macro ac:macro-id="43484e81-d0fd-4d40-b990-a7d90cd2b565" ac:name="panel" ac:schema-version="1"><ac:parameter ac:name="borderStyle">solid</ac:parameter><ac:rich-text-body>
<h4>&nbsp;<span style="color: rgb(51,51,51);">::__construct()</span></h4>
<hr />
<p><span style="color: rgb(51,51,51);">&nbsp;</span></p>
<p>Sets adapter options</p><ac:structured-macro ac:macro-id="323db0b7-4961-4b55-8332-43790ef9b2bc" ac:name="code" ac:schema-version="1"><ac:parameter ac:name="language">php</ac:parameter><ac:parameter ac:name="theme">Confluence</ac:parameter><ac:plain-text-body><![CDATA[__construct(mixed $filename = null, mixed $realm = null, mixed $identity = null, mixed $credential = null)]]></ac:plain-text-body></ac:structured-macro>
<h5>Parameters</h5><ac:structured-macro ac:macro-id="eda8fe1a-8ccd-4b7b-8942-99fd9c2dba18" ac:name="expand" ac:schema-version="1"><ac:rich-text-body>
<table>
<tbody>
<tr>
<th>Parameter</th>
<th>Data type</th>
<th>Description</th></tr>
<tr>
<td>$filename</td>
<td>mixed</td>
<td>&nbsp;</td></tr>
<tr>
<td>$realm</td>
<td>mixed</td>
<td>&nbsp;</td></tr></tbody></table></ac:rich-text-body></ac:structured-macro></ac:rich-text-body></ac:structured-macro></ac:layout-cell></ac:layout-section></ac:layout>

Поэтому, когда я использую его в вызове REST API, я получаю сообщение об ошибке, упомянутой ранее. Звонок выглядит так:

<?php

use GuzzleHttp\Client;
use GuzzleHttp\Post\PostBody;
use GuzzleHttp\Stream\StreamInterface;
use GuzzleHttp\Exception\RequestException;

function send_request() {
// My API (2) (POST http://confluence.dev/rest/api/content)

$client = new Client();
$request = $client->createRequest('POST', 'http://confluence.dev/rest/api/content');
$postBody = $request->getBody();

try {
$request->addHeaders([
'Authorization'=>'Basic XXX',
'Content-Type'=>'application/json',
]);
$body = [
"body" => [
"storage" => [
"value" => "<ac:layout-section ac:type=\"single\"><ac:layout-cell><ac:structured-macro ac:macro-id=\"3e74101c-857f-4c47-8618-a27c627eea2c\" ac:name=\"info\" ac:schema-version=\"1\"><ac:parameter ac:name=\"title\">Fully qualified class name</ac:parameter><ac:rich-text-body>\\Zend\\Authentication\\Adapter\\Digest</ac:rich-text-body></ac:structured-macro>
<p><a href=\"https://github.com/zendframework/zend-authentication/blob/master/src/Adapter/Digest.php\">Browse source code</a></p></ac:layout-cell></ac:layout-section><ac:layout-section ac:type=\"two_left_sidebar\"><ac:layout-cell>
<h2>Class contents</h2>
<p><ac:structured-macro ac:macro-id=\"726aa6fa-6e48-4005-95ad-0fb28aae8162\" ac:name=\"toc\" ac:schema-version=\"1\"><ac:parameter ac:name=\"maxLevel\">4</ac:parameter><ac:parameter ac:name=\"minLevel\">3</ac:parameter></ac:structured-macro></p>
<p>&nbsp;</p></ac:layout-cell><ac:layout-cell>
<h2>Description</h2>
<p><ac:structured-macro ac:macro-id=\"c7feab99-a70b-4ab8-bbac-846ed981bed0\" ac:name=\"excerpt\" ac:schema-version=\"1\"><ac:parameter ac:name=\"atlassian-macro-output-type\">INLINE</ac:parameter><ac:rich-text-body>
<p>Digest adapter for Zend Authentication mechanism</p></ac:rich-text-body></ac:structured-macro>&nbsp;</p>
<h2>Class details</h2>
<table style=\"letter-spacing: 0.1px;\">
<tbody>
<tr>&& count($stdBody) == 0
<th>Package</th>
<td>\\Zend\\Authentication\\Adapter</td></tr>
<tr>
<th>Inherited from</th>
<td><span style=\"color: rgb(68,68,68);\">\\Zend\\Authentication\\Adapter\\AbstractAdapter</span></td></tr></tbody></table>
<h3>Methods</h3>
<hr /><ac:structured-macro ac:macro-id=\"43484e81-d0fd-4d40-b990-a7d90cd2b565\" ac:name=\"panel\" ac:schema-version=\"1\"><ac:parameter ac:name=\"borderStyle\">solid</ac:parameter><ac:rich-text-body>
<h4>&nbsp;<span style=\"color: rgb(51,51,51);\">::__construct()</span></h4>
<hr />
<p><span style=\"color: rgb(51,51,51);\">&nbsp;</span></p>
<p>Sets adapter options</p><ac:structured-macro ac:macro-id=\"9de243a9-ca88-45e1-a6d4-7ff9b542babd\" ac:name=\"code\" ac:schema-version=\"1\"><ac:parameter ac:name=\"language\">php</ac:parameter><ac:parameter ac:name=\"theme\">Confluence</ac:parameter><ac:plain-text-body><![CDATA[__construct(mixed $filename = null, mixed $realm = null, mixed $identity = null, mixed $credential = null)]]></ac:plain-text-body></ac:structured-macro>
<h5>Parameters</h5><ac:structured-macro ac:macro-id=\"a2230db4-ea43-4e99-89d9-2661a57fd5c3\" ac:name=\"expand\" ac:schema-version=\"1\"><ac:rich-text-body>
<table>
<tbody>
<tr>
<th>Parameter</th>
<th>Data type</th>
<th>Description</th></tr>
<tr>
<td>$filename</td>
<td>mixed</td>
<td>&nbsp;</td></tr>
<tr>
<td>$realm</td>
<td>mixed</td>
<td>&nbsp;</td></tr></tbody></table></ac:rich-text-body></ac:structured-macro></ac:rich-text-body></ac:structured-macro></ac:layout-cell></ac:layout-section>",
"representation" => "storage"]
],
"title" => "Adam tests",
"space" => [
"key" => "TEST"],
"type" => "page"];
$request->setBody(GuzzleHttp\Stream\Stream::factory(json_encode($body)));

$response = $client->send($request);
echo "Response HTTP : " . $response->getStatusCode();
}
catch (RequestException $e) {
echo "HTTP Request failed\n";
echo $e->getRequest();
if ($e->hasResponse()) {
echo $e->getResponse();
}
}
}

Я использую Confluence 5.9.4.

Что-то не так с тем, как я пытаюсь создать эту страницу? Доступен ли формат хранения на странице, несовместимый с API?

2

Решение

Вы можете попробовать создать такой URL для формата хранения:

http://localhost:8099/rest/api/content/<pageID>?expand=body.storage,version

1

Другие решения

Насколько я могу судить, даже с мая 2017 года вы не можете использовать формат POST XML для Confluence. Ниже приведена моя попытка размещения XML-строки в Powershell, но с ошибками Bad Request. Попытка опубликовать пустой тег, как </ac:layout-cell><ac:layout-cell> выдаст ошибку неверного запроса, поэтому я предполагаю, что Confluence пытается проанализировать значение хранилища, и нам нужен способ предотвратить это, и только POST необработанное значение.

add-type @"using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

$uri = "https://confluence.dev/rest/api/content/"$headers = @{}
$headers.Add("Authorization", "Basic XXX")
$headers.Add("Content-Type", "application/json")

$temp = get-content .\temp.xml -raw
$body =  '{"type":"page","title":"Auto Gen Test","space":{"key":"SAN"},"body":{"storage":{"value":"'+($temp)+'","representation":"storage"}}}'

$result = Invoke-RestMethod -Method POST -Headers $headers -Uri $uri -Body $body
0