Andrew Fletcher published: 24 June 2022 2 minutes read
Whilst I had regularly used HAL to upload files through REST API, this is no longer an option given Hal is now deprecated and will be dropped in Drupal 10. Time to explore JSON:API.
Drupal modules to be installed
You will need to install
- JSON:API
- Media
Configuration for JSON:API
Alter the JSON:API to accept all:
Path: Administration > Configuration > Web services > JSON:API
Select: Accept all JSON:API create, read, update, and delete operations.
Configuration for Media
Path: Administration > People > Permissions
Under media add permissions for authenticated user
- Create media
- Update own media
Postman testing
Testing the image file upload I'll do through Postman and provide the configuration below.
POST http://{domain}/jsonapi/media/image/field_media_image/
Headers
Content-Type: application/octet-stream
Accept: application/vnd.api+json
X-CSRF-Token: {token}
Content-Disposition: file;filename="{filename}"
Body
Use binary and add an image
Response
A successful response will be:
{
"jsonapi": {
"version": "1.0",
"meta": {
"links": {
"self": {
"href": "http://jsonapi.org/format/1.0/"
}
}
}
},
"data": {
"type": "file--image",
"id": "f17322ca-6e4f-4c0b-83b7-e25f362c841c",
"links": {
"self": {
"href": "http://{domain}/jsonapi/file/image/f17322ca-6e4f-4c0b-83b7-e25f362c841c"
}
},
"attributes": {
"drupal_internal__fid": 8954,
"langcode": "en",
"filename": "image-32x32.png",
"uri": {
"value": "public://2022-06/image-32x32_0.png",
"url": "/sites/default/files/2022-06/image-32x32_0.png"
},
"filemime": "image/png",
"filesize": 105784,
"status": false,
"created": "2022-06-24T01:52:39+00:00",
"changed": "2022-06-24T01:52:39+00:00",
"path": {
"alias": null,
"pid": null,
"langcode": "en"
},
"metatag": null,
"field_image_alt_text": null,
"field_image_title_text": null
},
"relationships": {
"file_type": {
"data": {
"type": "file_type--file_type",
"id": "b91d750b-f6e9-4ae5-8d9c-a16f3ebcc23f",
"meta": {
"drupal_internal__target_id": "image"
}
},
"links": {
"related": {
"href": "http://{domain}/jsonapi/file/image/f17322ca-6e4f-4c0b-83b7-e25f362c841c/file_type"
},
"self": {
"href": "http://{domain}/jsonapi/file/image/f17322ca-6e4f-4c0b-83b7-e25f362c841c/relationships/file_type"
}
}
},
"uid": {
"data": {
"type": "user--user",
"id": "2703a587-307e-4f8b-8af8-77a3dc5887f8",
"meta": {
"drupal_internal__target_id": 200
}
},
"links": {
"related": {
"href": "http://{domain}/jsonapi/file/image/f17322ca-6e4f-4c0b-83b7-e25f362c841c/uid"
},
"self": {
"href": "http://{domain}/jsonapi/file/image/f17322ca-6e4f-4c0b-83b7-e25f362c841c/relationships/uid"
}
}
}
}
},
"links": {
"self": {
"href": "http://{domain}/jsonapi/file/image/f17322ca-6e4f-4c0b-83b7-e25f362c841c"
}
}
}
The key item of data to grab here is
{
"attributes": {
"drupal_internal__fid": 8954,
}
}
The file id {fid}
Related articles
Andrew Fletcher
•
20 Feb 2025
Handling duplicate records in Drupal
Duplicate records in Drupal can cause significant issues, particularly when they lead to integrity constraint violations in the database. These errors often occur due to duplicate UUIDs in the `node` table, which can result from programmatic imports, migrations, or unintended database...
Andrew Fletcher
•
17 Feb 2025
The overlooked challenge of migration updates in Drupal projects
When working on Drupal migrations, developers often assume that adding a new field to the process section will seamlessly update existing content. However, unless explicitly handled, Drupal’s migration system does not automatically apply changes to previously migrated records. This oversight can...
Andrew Fletcher
•
11 Feb 2025
Webpack build process and theme automation improvements
The Drupal theme configuration has undergone recent changes made to the Webpack configuration, SCSS and JavaScript handling, and automation of updates to the orw.libraries.yml file in the custom Drupal theme. These changes are designed to improve the build process, enhance maintainability, and...