espenmn
(Espen)
July 20, 2023, 3:21pm
1
Is there a restApi endpoint I can use to get (a richtext field of a ) content item if I have the UID ?
Similar to plone.api.content.get( UID=Something )
UPDATE; It is OK to get the text without any markup, if that is an option.
davisagli
(David Glick)
July 20, 2023, 3:43pm
2
You can fetch /resolveuid/UID to get a redirect to the content item's current path, and then get /++api++/path/to/item to get that item's REST API representation.
espenmn
(Espen)
July 20, 2023, 4:29pm
3
davisagli:
REST API representation
Thanks. I hoped there was something 'more direct'.
My current approach is to search for all and slice the result. Works, but does not feel 'optimal'. I tested a (jQuery) script, and it seems to work:
$('#form-widgets-related_sow_section').on( "change", function() {
if ($( this ).val() != '--NOVALUE--' ) {
$searchval = $('body').attr("data-portal-url") + '/@search?fullobjects=1&UID='+ $( this ).val() ;
$.ajax({
url: $searchval,
contentType: "application/json",
dataType: 'json',
headers: {'Accept': 'application/json'},
success: function(result){
//console.log(result.items[0]);
$('#sow_text_add').remove();
$( "#formfield-form-widgets-related_sow_section").append('<div id="sow_text_add">' + result.items[0].bodytext.data + '</div>');
}
})
}
davisagli
(David Glick)
July 20, 2023, 4:44pm
4
To me, searching by UID and returning fullobjects sounds like a reasonable way to do it.
mekell
(me-kell)
July 20, 2023, 7:42pm
6
Be careful: The RESTAPI has an unfixed bug that affects richtext fields of whatever mime_type. If your content uses mime_types other than html the RESTAPI is unusable until this bug is fixed:
opened 09:24AM - 25 Oct 22 UTC
01 type: bug
21 status: confirmed
The REST API returns the HTML-Version of richttext fields even if the `content-t… ype` is set to `text/x-web-markdown`.
I'd expect the stored markdown version (`value.raw`) to be returned.
We send:
```json
"text": {
"content-type": "text/x-web-markdown",
"data": "xyz **bold** normal",
"encoding": "utf8"
}
```
We get:
```json
"text": {
"content-type": "text/x-web-markdown",
"data": "<p>xyz <strong>bold</strong> normal</p>",
"encoding": "utf8"
}
```
**To reproduce it:**
```shell
SITE_URL=https://6-classic.demo.plone.org
USER=manager:**** # <- set manager password here
cat <<EOF | tee config_markup.json
{
"allowed_types": [
{"title":"text/html","token": "text/html"},
{"title": "text/x-web-markdown","token": "text/x-web-markdown"},
{"title": "text/x-web-textile","token": "text/x-web-textile"}
],
"default_type": {"title":"text/html","token": "text/html"},
"markdown_extensions":[
"markdown.extensions.fenced_code",
"markdown.extensions.footnotes",
"markdown.extensions.tables"
]
}
EOF
curl -i -X PATCH "${SITE_URL}/@controlpanels/markup" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
--data-binary "@my_document.json" \
--user ${USER}
cat <<EOF | tee my_document.json
{
"@type": "Document",
"title": "MyDocument",
"text": {
"content-type": "text/x-web-markdown",
"data": "xyz **bold** normal",
"encoding": "utf8"
}
}
EOF
ID=$(
curl -s -X POST "${SITE_URL}" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
--data-binary "@my_document.json" \
--user ${USER} \
| jq -r '."@id"' \
)
echo $ID
curl -s -X GET "${ID}" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
--user ${USER} \
| jq -r '."text"'
```
It returns
```json
...
"text": {
"content-type": "text/x-web-markdown",
"data": "<p>xyz <strong>bold</strong> normal</p>",
"encoding": "utf8"
}
...
```