How to upload Attachments via API and python requests?

Hi,

I’m running Deck 1.5.5 on NC 22.2.3.
I have a python script that takes information from another site and creates cards automatically. Now I want to also generate PDFs and add them to these cards.

The documentation tells me to post to the endpoint
/boards/{boardId}/stacks/{stackId}/cards/{cardId}/attachments
and use parameters type and file.

As I said I am using python and the requests package. Creating the cards works like charm, however I am unsure of how to pass the file into the requests.post() call. I’m only just starting out with python.

I have tried both the simpler version and the tuple version of what requests doc says, using the files argument and as the file item in the dict passed as params.
This gives me code 500: No file uploaded or file size exceeds maximum of 512MB, which of course it didn’t.

requests.post('https://<URL>index.php/apps/deck/api/v1.0/boards/<boardId>/stacks/<stackId>/cards/<cardId>/attachments', headers={'OCS-APIRequest':'true', 'Content-Type': 'application/json'}, auth=("***","***"), data={'type':'file'}, files={'file': ('file.pdf', open('file.pdf', 'rb'), 'application/pdf')})

requests.post(https://<URL>/index.php/apps/deck/api/v1.0/boards/<boardId>/stacks/<stackId>/cards/<cardId>/attachments', headers={'OCS-APIRequest':'true', 'Content-Type': 'application/json'}, auth=("***","***"), params={'type':'file', 'file': ('file.pdf', open('file.pdf', 'rb'), 'application/pdf')})

So is 'type':'file' correct and how do I need to package, let’s say, file.pdf in order to pass it as the file parameter?

Thank you,
pmallot

Hey :wave:

unfortunately i can’t give you a direct solution, but in the Deck Android app we are using the following endpoints - maybe you can read some difference to your approach?

https://github.com/stefan-niedermann/nextcloud-deck/blob/master/app/src/main/java/it/niedermann/nextcloud/deck/api/DeckAPI.java#L146

Hi Stefan,
thanks for the answer, unfortunately I didn’t really find anything helpful to me.

I have tried getting an existing attachment to see what the API returns. However this didn’t work either.

r=requests.get("https://***/index.php/apps/deck/api/v1.1/boards/18/stacks/55/cards/443/attachments/201", headers={'OCS-APIRequest':'true', 'Content-Type': 'application/json'}, auth=("***","***"))

This returns a code 405 Method Not Allowed and what seems to be an empty binary object: b''

Making the same request to the v1.0 API results in a response 500 Internal Server Error with the Nextcloud logs saying the following:

[deck] Error: Object of class OC\DB\QueryBuilder\QueryBuilder could not be converted to string

GET /index.php/apps/deck/api/v1.0/boards/18/stacks/55/cards/443/attachments/201
from *** by *** at ***
Exception: Object of class OC\DB\QueryBuilder\QueryBuilder could not be converted to string
/var/www/html/lib/private/AppFramework/App.php - line 156:

OC\AppFramework\Http\Dispatcher->dispatch(OCA\Deck\Con ... {}, "display")

/var/www/html/lib/private/Route/Router.php - line 302:

OC\AppFramework\App::main("OCA\\Deck\\ ... r", "display", OC\AppFramew ... {}, { 0: "And 1 ... "})

/var/www/html/lib/base.php - line 1006:

OC\Route\Router->match("/apps/deck/ ... 1")

/var/www/html/index.php - line 36:

OC::handleRequest()

Verursacht durch Error: Object of class OC\DB\QueryBuilder\QueryBuilder could not be converted to string
/var/www/html/custom_apps/deck/lib/Service/AttachmentService.php - line 238:

OCA\Deck\Db\AttachmentMapper->find("201")

/var/www/html/custom_apps/deck/lib/Controller/AttachmentApiController.php - line 62:

OCA\Deck\Service\AttachmentService->display("443", "201", "deck_file")

/var/www/html/lib/private/AppFramework/Http/Dispatcher.php - line 217:

OCA\Deck\Controller\AttachmentApiController->display("443", "201", "deck_file")

/var/www/html/lib/private/AppFramework/Http/Dispatcher.php - line 126:

OC\AppFramework\Http\Dispatcher->executeController(OCA\Deck\Con ... {}, "display")

/var/www/html/lib/private/AppFramework/App.php - line 156:

OC\AppFramework\Http\Dispatcher->dispatch(OCA\Deck\Con ... {}, "display")

/var/www/html/lib/private/Route/Router.php - line 302:

OC\AppFramework\App::main("OCA\\Deck\\ ... r", "display", OC\AppFramew ... {}, { 0: "And 1 ... "})

/var/www/html/lib/base.php - line 1006:

OC\Route\Router->match("/apps/deck/ ... 1")

/var/www/html/index.php - line 36:

OC::handleRequest()

The ids for board, stack, card and id are correct. However I was only able to get a list of attachments when using the v1.1 api. I understand the file storage was changed moving to v1.1 so I assume this is more about the documentation not being updated yet.