Adding tags results in 422 error in shelve, book, chapter, page API requests #4844

Closed
opened 2026-02-05 09:20:11 +03:00 by OVERLORD · 2 comments
Owner

Originally created by @ShajeeAmjad on GitHub (Jul 1, 2024).

Describe the Bug

I've been using Bookstack for development for a while and was wanting to add some tags to my content programmatically. However no matter what I do, it never works. I have tried the example data on the API docs, but even still it does not work.

Here is the code I am using:

`

def create_shelf(api_url, api_token):
      shelf_url = f"{api_url}/shelves"
      headers = {
          'Authorization': f'Token {api_token}',
      }

    data = {
        "name": "My shelf",
        "description_html": "<p>This is <strong>my shelf</strong> with some books</p>",
        "books": [],
        "tags": [
          {"name": "Category", "value": "Learning"}
        ]
    }

    response = requests.post(shelf_url, headers=headers, data=data)
    
    if response.status_code == 200:
        return response.json()
    else:
        print(f"Failed to create shelf. Status code: {response.status_code}")
        print(response.content.decode('utf-8'))
        return None

`

The result is always: Error 422, the data given was invalid, tags must be an array. Even though tags is clearly an array.

I have no clue what the problem is.

Steps to Reproduce

Make the API request as above.

Expected Behaviour

Should create a shelf with the given tags

Screenshots or Additional Context

No response

Browser Details

FireFox, latest version

Exact BookStack Version

Bookstack latest version

Originally created by @ShajeeAmjad on GitHub (Jul 1, 2024). ### Describe the Bug I've been using Bookstack for development for a while and was wanting to add some tags to my content programmatically. However no matter what I do, it never works. I have tried the example data on the API docs, but even still it does not work. Here is the code I am using: ` def create_shelf(api_url, api_token): shelf_url = f"{api_url}/shelves" headers = { 'Authorization': f'Token {api_token}', } data = { "name": "My shelf", "description_html": "<p>This is <strong>my shelf</strong> with some books</p>", "books": [], "tags": [ {"name": "Category", "value": "Learning"} ] } response = requests.post(shelf_url, headers=headers, data=data) if response.status_code == 200: return response.json() else: print(f"Failed to create shelf. Status code: {response.status_code}") print(response.content.decode('utf-8')) return None ` The result is always: Error 422, the data given was invalid, tags must be an array. Even though tags is clearly an array. I have no clue what the problem is. ### Steps to Reproduce Make the API request as above. ### Expected Behaviour Should create a shelf with the given tags ### Screenshots or Additional Context _No response_ ### Browser Details FireFox, latest version ### Exact BookStack Version Bookstack latest version
OVERLORD added the 🐛 Bug label 2026-02-05 09:20:11 +03:00
Author
Owner

@ssddanbrown commented on GitHub (Jul 1, 2024):

Hi @ShajeeAmjad,

Even though tags is clearly an array.

That would totally depend on how the requests library serializes the post data from the Python array, which I'd imagine to be different to the format BookStack accepts.

Instead, try sending the data as JSON:

response = requests.post(shelf_url, headers=headers, json=data)
@ssddanbrown commented on GitHub (Jul 1, 2024): Hi @ShajeeAmjad, > Even though tags is clearly an array. That would totally depend on how the requests library serializes the post data from the Python array, which I'd imagine to be different to the format BookStack accepts. Instead, try sending the data as JSON: ```python response = requests.post(shelf_url, headers=headers, json=data) ```
Author
Owner

@ShajeeAmjad commented on GitHub (Jul 1, 2024):

That seems to have worked, thank you for the quick response.

@ShajeeAmjad commented on GitHub (Jul 1, 2024): That seems to have worked, thank you for the quick response.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/BookStack#4844