Is it possible to import a json file using python and the rest API onto a page?

Hello everyone,
I’m currently trying to write a script to import a json file onto a wikipage using the rest API so I was wondering if that is even possible. I read through the rest API docs and I didn’t find any Method that allows json files, only xml.

I’m very new to XWiki and the rest API so I could use a little bit of guidance.

I was thinking I could convert the json file to a xml file and then have the xml file be uploaded to e.g. create a table on the page. So I wrote a little code to POST the xml onto the page, but I got returned the 405 error (Method not allowed).
Here is the code in question:

import requests
import xml.etree.ElementTree as ET

url = "http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/Sandbox/pages/TestPage1"
file_path = r"example.xml"

username = "user"
password = "pass"

headers = {
    "Content-Type": "application/xml",
    "Accept": "application/xml",
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36'

}

with open(file_path, "rb") as file:

    response = requests.post(url, headers=headers, data=file, auth=(username, password), allow_redirects=True)

if response.status_code == 201:
    print("The Page was successfully created.")
elif response.status_code == 202:
    print("The page was successfully updated.")
elif response.status_code == 304:
    print("The Page was not modified.")
elif response.status_code == 401:
    print("The user is not authorized.")
else:
    print("Error uploading the data:", response.status_code)

However, this (as I just mentioned) returns the error code 405.
Does anyone know how to solve this? This is basically the example given in the API docs with curl:
$ curl -u Admin:admin -X POST -H "Content-type: application/xml" -H "Accept: application/xml" -d "@test.xml" http://localhost/xwiki/rest/wikis/xwiki/spaces/Test/pages/Test/objects
but instead with python. I even copied the xml file but it doesn’t work yet. How can I resolve this?
Thank you in advance!

Salute, have you tried PUT method according to the documentation of XWiki REST Api, also there’s an example of curl command REST API (XWiki.org) - Sending representations. In addition, I see that your if/elif/else statement isn’t indented (in current example Python interpreter shall raise an error NameError: name 'response' is not defined), you can print extra useful information for each status code print(response.text)