Write to a page from code

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

Read the plone,restapi docs instead of using ChatGPT.
We are not here fix for fixing ChatGPT‘s nonsense code.

1 Like

oops, sorry. I found it amusing, not so much on here.

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

If you run the code from the same machine, plone.api might be simpler (?)

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.

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

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

Text can be sent with a POST request when creating a resource.

Once the resource exists text can be updated with PATCH request

This part creates content. It can be done from a script (python)

This looks like it must run on the site server ...

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' ?

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

Great, now I have what I need!

Thanks,
Kent

1 Like

Yes, it has to be run on server