Help to understand a complete workflow for creating pages with images using API
Attempted Debugging
- [X] I have read the debugging page
Searched GitHub Issues
- [X] I have searched GitHub for the issue.
Describe the Scenario
Hi, I am trying to create a page using the API. The page must have images. Steps:
- Exported a complete Confluence space with attachments, in HTML format.
- Using Jupyter and Python
requestslibrary, I created a page using HTML as a body. - There've been a link to an image inside this HTML:
<span class="confluence-embedded-file-wrapper"><img class="confluence-embedded-image" draggable="false" src="attachments/104497408/104497409.jpg" data-image-src="attachments/104497408/104497409.jpg" data-unresolved-comment-count="0" data-linked-resource-id="104497409" data-linked-resource-version="1" data-linked-resource-type="attachment" data-linked-resource-default-alias="(Docu User) Asset page concept 2018.3.jpg" data-base-url="https://confluence.company.com" data-linked-resource-content-type="image/jpeg" data-linked-resource-container-id="104497408" data-linked-resource-container-version="1" alt=""></span> - As a result, when I check the created page in Bookstack, instead of this image, there is an empty line. In the code of the page, I see the link from above. At the bottom of the page, the image is referenced again as an attachment, but there is no one (it opens a 404 page). My question is: should I upload the image before creating the page? I tried to upload image as image and as attachment following your API documentation. But I am not sure what is wrong with my call:
image_url = 'https://bookstack.company.com/api/image-gallery'
payload={
"type":"gallery",
"uploaded_to":36,
"image":{
"file":"/Users/user/htmlexport-20231222-135352-809/CS20231PAR/attachments/104497408"
}
}
response = requests.post(
image_url,
headers=headers,
data=payload
)
Throws an error:
{'error': {'message': 'Call to a member function getClientOriginalExtension() on string',
'code': 500}}
Tried to upload the image as an attachment:
attach_url='https://bookstack.company.com/api/attachments'
payload={
"name":"my image",
"uploaded_to":36,
"file": "/Users/user/htmlexport-20231222-135352-809/CS20231PAR/attachments/104497408"
}
response = requests.post(
attach_url,
headers=headers,
data=payload
)
Throws an error:
{'error': {'message': 'The given data was invalid.',
'validation': {'name': ['The name field is required.'],
'file': ['The file must be provided as a valid file.']},
'code': 422}}
Please help me to understand what is wrong with my requests or maybe with the order in general (first upload images then create a page, but you need a page id to upload an image, right?)
Thank you!
Exact BookStack Version
v23.08.3
Log Content
No response
Hosting Environment
Bookstack is currently running on a VM that uses the following hardware:
- 2 vCPUs (AMD64)
- 8 GiB RAM
- 100GiB total space on a local filesystem
Software:
- OS: Debian 12 Bookworm
- nginx/1.22.1 (WebServer/Reverse Proxy)
- PHP8.2
- MariaDB 10.11.3
It has been installed using the official installation script.
It looks like you are sending the "file location" in your API request. Even if you send the location in your computer, the server cannot see the contents of your computer.
You need to have a good grasp of what data you need to send with the API. This also requires knowledge of HTTP. A sample code that probably matches what you are looking for can be found here. https://github.com/BookStackApp/api-scripts
If you find it difficult, you can use a wrapper library.