How to create a content with files attached through API

I have…

I’m submitting a…

  • [ ] Regression (a behavior that stopped working in a new release)
  • [ ] Bug report
  • [ ] Performance issue
  • [ ] Documentation issue or request

Current behavior

I’m able to create new content with REST API but I’m not sure how to attach a file/image to it.

Expected behavior

Minimal reproduction of the problem

I’m using Python requests with the REST API (code snippet)

import requests
headers={
          'Content-Type': 'application/json', #this should be multipart/form-data once I add a file.
          'Authorization': 'Bearer ' + token
        }

payload = json.dumps({
                        "dni": {
                            "iv": row['dni']
                        },
                        "nombres": {
                            "iv": row['nombres']
                        }})

response = requests.post(self.api_url + "/api/content/atlas/padron/?publish=true", headers=headers, data=payload, verify=False)

Response:

{‘total’: 1, ‘items’: [{‘id’: ‘da05bdaf-1ab4-4878-a80e-bab608e8c4f9’, ‘createdBy’: ‘subject:6081f499800b8c7454f0fd73’, ‘lastModifiedBy’: ‘subject:6081f499800b8c7454f0fd73’, ‘data’: {‘dni’: {‘iv’: None}, ‘nombres’: {‘iv’: None}, ‘created’: ‘2021-04-23T03:30:20Z’, ‘lastModified’: ‘2021-04-23T03:30:20Z’, ‘status’: ‘Published’, ‘statusColor’: ‘#4bb958’, ‘schemaName’: ‘padron’, ‘schemaDisplayName’: ‘padron’, ‘version’: 1, ‘_links’: {‘self’: {‘href’: ‘/api/content/atlas/padron/da05bdaf-1ab4-4878-a80e-bab608e8c4f9’, ‘method’: ‘GET’}, ‘previous’: {‘href’: ‘/api/content/atlas/padron/da05bdaf-1ab4-4878-a80e-bab608e8c4f9/0’, ‘method’: ‘GET’}, ‘draft/create’: {‘href’: ‘/api/content/atlas/padron/da05bdaf-1ab4-4878-a80e-bab608e8c4f9/draft’, ‘method’: ‘POST’}, ‘delete’: {‘href’: ‘/api/content/atlas/padron/da05bdaf-1ab4-4878-a80e-bab608e8c4f9’, ‘method’: ‘DELETE’}}}], ‘statuses’: [{‘status’: ‘Archived’, ‘color’: ‘#eb3142’}, {‘status’: ‘Draft’, ‘color’: ‘#8091a5’}, {‘status’: ‘Published’, ‘color’: ‘#4bb958’}], ‘_links’: {‘self’: {‘href’: ‘/api/content/atlas/padron’, ‘method’: ‘GET’}, ‘create’: {‘href’: ‘/api/content/atlas/padron’, ‘method’: ‘POST’}, ‘create/publish’: {‘href’: ‘/api/content/atlas/padron?publish=True’, ‘method’: ‘POST’}}}

Now I add a “photo”(asset field) and the Swagger API Docs specification says that “photo” field is:

“photo”: {
“iv”:[“string”]
}

Which is not clear for me how to handle it for a correct image upload related to that content row.

Hope it makes sense to you. Thanks in advance Sebastian!

Environment

  • [ X ] Self hosted with docker
  • [ ] Self hosted with IIS
  • [ ] Self hosted with other version
  • [ ] Cloud version

Version: [VERSION]

Browser:

  • [ ] Chrome (desktop)
  • [ ] Chrome (Android)
  • [ ] Chrome (iOS)
  • [ ] Firefox
  • [ ] Safari (desktop)
  • [ ] Safari (iOS)
  • [ ] IE
  • [ ] Edge

Others:

When you upload the asset you get the asset object back. You take the ID and assign as a an array to the asset field:

https://docs.squidex.io/02-documentation/concepts/schemas#assets

Thanks a lot Sebastian, sorry for my bad research!

1 Like