Skip to main content

To POST an image is similar to the other posts mentioned on this site.  This article works through the steps to post an image.

To be able to get, patch or post you need to log in the member so you can retrieve their user id (uid).

Calling a query

To POST an image, the details required are:

  • Authentication credentials;
  • Base64image - converted;
  • Directory_path - the directory path is constructed by combining two directories:
    • pictures/date, where date is prepresented as year-month
    • So for the year 2017 and month August 08, the directory path is pictures/2017-08;
  • Filename; and
  • User uid

The request string is: {domain}/entity/file?_format=hal_json

See the notes on set up, for the staging and production URLs.  Using the current staging URL in the set up screen, as example of the query string using the following criteria

Body
{"_links":{
"type":{"href": "https://{domain}/rest/type/file/image"}
},
"filename":[{"value":"{filename}"}],
"filemime":[{"value":"image/jpeg"}],
"type": [{"target_id": "image"}],
"uid": [{"target_id": "{uid}"}],
"uri": [{"value": "public://{directory_path}/{filename"}],
"data":[{"value":"{base64image"}]
}

Replacing the following variables:

  • {base64image} - The image file needs to be converted to a base64 code
  • {directory_path} - pictures/2017-08
  • {domain} ~ stg.runningrival.com
  • {filename} - sarah01.jpg
  • {uid} - 5
https://your-site.com/entity/file?_format=hal_json
Body
{"_links":{
"type":{"href": "https://your-site.com/rest/type/file/image"}
},
"filename":[{"value":"sarah01.jpg"}],
"filemime":[{"value":"image/jpeg"}],
"type": [{"target_id": "image"}],
"uid": [{"target_id": "5"}],
"uri": [{"value": "public://pictures/2017-08/sarah01.jpg"}],
"data":[{"value":""}]
}
Response output

The response by posting the above will be 201 Created.  Additionally other details in the response will be similar to:

{
    "_links":
    {
        "self":
        {
            "href": "https://your-site.com/file/5?_format=hal_json"
        },
        "type":
        {
            "href": "https://your-site.com/rest/type/file/image"
        },
        "https://your-site.com/rest/relation/file/image/uid": [
        {
            "href": "https://your-site.com/user/5?_format=hal_json"
        }]
    },
    "fid": [
    {
        "value": 5
    }],
    "uuid": [
    {
        "value": "ea63b5e8-3f47-4320-aad9-bfe835d4f8ab"
    }],
    "langcode": [
    {
        "value": "en"
    }],
    "type": [
    {
        "target_id": "image"
    }],
    "_embedded":
    {
        "https://your-site.com/rest/relation/file/image/uid": [
            {
                "_links":
                {
                    "self":
                    {
                        "href": "https://your-site.com/user/5?_format=hal_json"…
                    },
                    "uuid": [
                    {
                        "value": "5ba2e4c0-2ca3-a3b2c-aec8-2984348cd4db"
                    }]
                }
            ]
        },
        "filename": [
        {
            "value": "sarah7.jpg"
        }],
        "uri": [
        {
            "value": "public://pictures/2017-08/sarah7_0.jpg"
        }],
        "filemime": [
        {
            "value": "image/jpeg"
        }],
        "filesize": [
        {
            "value": 35291
        }],
        "status": [
        {
            "value": true
        }],
        "created": [
        {
            "value": 1502676875
        }],
        "changed": [
        {
            "value": 1502676875
        }],
        "path": [
        {
            "alias": "",
            "pid": null,
            "lang": "en"
        }],
        "data": [
        {
            "value": "/9j/4QA2RXhpZgAASUkqAAgAAAABAJiCAgARAAA...."
        }]
    }

Note the data value has been understandably truncated.

One of the important values to retrieve from this output is the fid value.  In this instance that value is five (5).

Related articles

Andrew Fletcher15 Mar 2024
Could not apply patch! Skipping
Attempting to apply a patch in Drupal 10.2.4 that had been working until this version.  Now I'm seeing the following errorCould not apply patch! Skipping. The error was: The process "patch '-p1' --no-backup-if-mismatch -d '/Users/andrewfletcher/Sites/codebales/modules/contrib/facets' <...