ktenney
(Kent Tenney)
April 28, 2023, 6:31pm
1
I'm want to put text on a page (or create a page in a folder) from code
ChatGPT says first I need an auth token:
import requests
url = 'Springfed '
payload = {'login':'user', 'password':'pwd'}
response = requests.post(url, json=payload)
token = response.json().get('token')
...
2 issues:
response.json()
throws an error
...
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
response.content includes the string:
Cookies are not enabled. You must enable cookies before you can log in
Where should I look for an example of doing this?
Thanks,
Kent
1 Like
zopyx
(Andreas Jung)
April 28, 2023, 6:34pm
2
Read the plone,restapi docs instead of using ChatGPT.
We are not here fix for fixing ChatGPT‘s nonsense code.
1 Like
ktenney
(Kent Tenney)
April 28, 2023, 6:38pm
3
oops, sorry. I found it amusing, not so much on here.
ktenney
(Kent Tenney)
April 28, 2023, 7:08pm
4
from https://plonerestapi.readthedocs.io/en/latest/usage/content.html
response = requests.post('https://springfed.com/gather-1',
headers={'Accept': 'application/json',
'Content-Type': 'application/json'},
json={'@type': 'Document', 'title': 'My Document'},
auth=('user', 'pwd'))
response.json()
{'message': "Missing 'plone.restapi: Use REST API' permission",
'type': 'Unauthorized'}
Suggestions?
Thanks,
Kent
espenmn
(Espen)
April 29, 2023, 2:44pm
6
If you run the code from the same machine, plone.api might be simpler (?)
zopyx
(Andreas Jung)
April 29, 2023, 3:04pm
7
Wild guess: your user account does not have the right/permissions to use the plone.restapi
Apart from that: requests.post()
takes a URL...not use what Sprintfed
should be..the code does not make much sense.
ktenney
(Kent Tenney)
April 29, 2023, 5:07pm
8
Apart from that: requests.post()
takes a URL.
I wrote a URL and it was converted to "Springfed" by the editor
can I edit in plain text mode, or do URLs need 'preformatted text' applied?
yesterday I noticed that the plone.restapi add-on wasn't installed, things
have changed since it's installed
I can now get a token per Authentication — plone.restapi v8.24.2.dev0 and
use the token to create documents in a folder.
The next challenge is to send text to the new page, or can the POST statement also specify content?
Thanks,
Kent
ktenney
(Kent Tenney)
April 29, 2023, 5:10pm
9
This is for computer recycling.
The script will be on a bootable USB which runs a tool which
gathers hardware data, generates an ID for the computer
and creates a page with that ID containing the hardware specs,
so it can't run on the server.
Thanks,
Kent
mekell
(me-kell)
April 29, 2023, 5:32pm
10
Text can be sent with a POST
request when creating a resource.
Once the resource exists text can be updated with PATCH
request
plone.restapi not only exposes content objects via a RESTful API, but the API consumer can create, read, update, and delete a content object.
espenmn
(Espen)
April 29, 2023, 6:45pm
11
This part creates content. It can be done from a script (python)
portal_type = ctr.findTypeName(name.lower(), content_type, '')
if force_files and portal_type not in ('File', 'Image'):
portal_type = 'File'
elif not portal_type:
portal_type = 'File'
normalizer = getUtility(IFileNameNormalizer)
chooser = INameChooser(self.context)
newid = chooser.chooseName(normalizer.normalize(name), self.context.aq_parent)
obj = plone.api.content.create(container=container, type=portal_type, id=newid, title=name)
primary_field = IPrimaryFieldInfo(obj)
if isinstance(primary_field.field, RichText):
setattr(obj, primary_field.fieldname, RichTextValue(data))
else:
setattr(obj, primary_field.fieldname, primary_field.field._type(data, filename=utils.safe_unicode(name)))
modified(obj)
ktenney
(Kent Tenney)
April 29, 2023, 8:14pm
12
This looks like it must run on the site server ...
ktenney
(Kent Tenney)
April 29, 2023, 8:19pm
13
the description and example are aimed at metadata, the example changes the title
I need to access the text on the page
does the json in the PATCH call offer an index like 'body': or 'content' or 'text' ?
mekell
(me-kell)
April 29, 2023, 9:15pm
14
Documents have text
.
Try something like the following (omit the fields you don't want to change):
requests.patch(
'http://nohost/plone/folder/my-document',
headers={'Accept': 'application/json', 'Content-Type': 'application/json'},
json={
'title': 'Your new title',
"description": "Your new description",
"text": {
"content-type": "text/html",
"data": "your text <b>bold text</b>",
"encoding": "utf-8"
}
},
auth=('admin', 'secret')
)
To see the attributes in your resource, you could simply call requests.get('http://nohost/plone/folder/my-document', headers={'Accept': 'application/json'}, auth=('admin', 'secret'))
as described in the GET request and read the structure of the json result.
1 Like
ktenney
(Kent Tenney)
April 30, 2023, 5:58pm
15
Great, now I have what I need!
Thanks,
Kent
1 Like
espenmn
(Espen)
April 30, 2023, 7:34pm
16
Yes, it has to be run on server