Skip to main content

 

JSON structure

Outlining the structure of the elements common attributes: type, title, name and required;

Text area

{
    "type": "webform_multiple",
    "title": "What's new?",
    "name": "what_s_new_",
    "required": false
}

Text field

An additional attribute for the text field is maxlength

{
    "type": "textfield",
    "title": "Text field (optional)",
    "name": "text_field_optional_",
    "required": false,
    "maxlength": 255
}

Select field

Options attribute is an additional feature:

{
    "type": "select",
    "title": "Dropdown field (optional)",
    "name": "dropdown_field_optional_",
    "required": false,
    "options": [
        {
            "label": "Test 1",
            "name": "dropdown_field_optional_",
            "value": 1,
            "description": null
        },
        {
            "label": "Test 2",
            "name": "dropdown_field_optional_",
            "value": 2,
            "description": null
        },
        {
            "label": "Test 3",
            "name": "dropdown_field_optional_",
            "value": 3,
            "description": null
        }
    ]
}

Checkboxes and radios

The key difference between checkboxes and radio buttons is determined in the type.

{
    "type": "checkboxes",
    "title": "Category tags",
    "name": "category_tags",
    "required": false,
    "options": [
        {
            "label": "Business Update",
            "name": "category_tags[Business Update]",
            "value": "Business Update",
            "description": null
        },
        {
            "label": "Social Update",
            "name": "category_tags[Social Update]",
            "value": "Social Update",
            "description": null
        },
        {
            "label": "Compliment a colleague",
            "name": "category_tags[Compliment a colleague]",
            "value": "Compliment a colleague",
            "description": null
        }
    ]
}

Upload a file

This element utilises the Webform Dropzone JS module.  In the Drupal admin, the files that can be uploaded has been restricted.

{
    "type": "webform_dropzonejs",
    "title": "Upload image or file",
    "name": "upload_image_or_file",
    "value": {
        "uploaded_files": []
    },
    "description": {
        "file_upload_help": {
            "#theme": "file_upload_help",
            "#upload_validators": {
                "file_validate_size": [
                    33554432
                ],
                "file_validate_extensions": [
                    "jpg png pdf docx"
                ],
                "webform_file_validate_extensions": [],
                "webform_file_validate_name_length": []
            },
            "#cardinality": 1
        }
    },
    "required": false,
    "max_filesize": 33554432,
    "upload_validators": {
        "file_validate_size": [
            33554432
        ],
        "file_validate_extensions": [
            "jpg",
            "png",
            "pdf",
            "docx"
        ],
        "webform_file_validate_extensions": [],
        "webform_file_validate_name_length": []
    }
}

Capture element

{
    "captcha": "",
    "form_build_id": "<input autocomplete=\"off\" data-drupal-selector=\"form-hsvvfwmn9ntlsrq0jwabuco7l-a2yhja-5fgafiesa\" type=\"hidden\" name=\"form_build_id\" value=\"form--hsVVFWmn9NtLSRq0JWabuCO7l-A2YhJA-5fgafIeSA\" />\n",
    "form_id": "<input data-drupal-selector=\"edit-webform-submission-share-an-update-node-6637-add-form\" type=\"hidden\" name=\"form_id\" value=\"webform_submission_share_an_update_node_6637_add_form\" />\n",
    "form_token": "<input data-drupal-selector=\"edit-webform-submission-share-an-update-node-6637-add-form-form-token\" type=\"hidden\" name=\"form_token\" value=\"CTEGuVjB7ugGz2_zBeaCqFwMFoMP5innTuCyLekk1jE\" />\n",
    "submit": {
        "name": "op"
    },
    "ajax": "/mhc_custom/webform",
    "captcha_token": "",
    "captcha_sid": "",
    "captcha_sitekey": "6LcrVMgaAAAAABq8-xq48s1Rn4U6LD55YFIrf0tY",
    "webform_id": "share_an_update",
    "errors": [],
    "success": false,
    "raw_values": {
        "form_build_id_raw": "form--hsVVFWmn9NtLSRq0JWabuCO7l-A2YhJA-5fgafIeSA",
        "form_id_raw": "webform_submission_share_an_update_node_6637_add_form",
        "form_token_raw": "form_token_placeholder_obDsIMHgbrfCHSgI2LDlOr8T_qS0ISqOnNr4G_9n_dA"
    }   
}

 

Setting up the REST API

Using the references from above, let's set-up the POST REST call.

URL

POST  http://{domain}/mhc_custom/webform?_format=json

Headers

Content-Type: application/json
Accept: application/json
X-CSRF-Token: {token}

Body

{
    "webform_id": "share_an_update",
    "what_s_new_[what_value]": "what_value",
    "text_field_optional_": "{text_field_optional_value}",
    "dropdown_field_optional_": "{dropdown_field_optional_value}",
    "category_tags[Business Update]": "{boolean}",
    "category_tags_radio": "{category_tags_radio_value}",
    "upload_image_or_file": "{fid}"
}

Each of the values is noted from the options above.  However, how to get the {fid} value?

You need to upload the relevant file first, then run this REST POST.  Therefore, {fid} is the file id reference. 

Example of the values substituted

{
    "webform_id": "share_an_update",
    "what_s_new_[happening to us]": "happening to me",
    "text_field_optional_": "key one liner",
    "dropdown_field_optional_": "2",
    "category_tags[Business Update]": true,
    "category_tags[Compliment a colleague]": false,
    "category_tags[Social Update]": false,
    "category_tags_radio": "Second option",
    "upload_image_or_file": "8943"
}

 

Response

The response will be a sid number as a string. 

{
    "sid": "{sid}"
}

The response for {sid} will be a number like 142

To learn how to upload an image read - Upload an image file using JSON:API

Related articles

Andrew Fletcher04 Apr 2025
Managing .gitignore changes
When working with Git, the .gitignore file plays a critical role in controlling which files and folders are tracked by version control. Yet, many developers are unsure when changes to .gitignore take effect and how to manage files that are already being tracked. This uncertainty can lead to...
Andrew Fletcher26 Mar 2025
How to fix the ‘Undefined function t’ error in Drupal 10 or 11 code
Upgrading to Drupal 10.4+ you might have noticed a warning in their code editor stating “Undefined function ‘t’”. While Drupal’s `t()` function remains valid in procedural code, some language analysis tools — such as Intelephense — do not automatically recognise Drupal’s global functions. This...
Andrew Fletcher17 Mar 2025
Upgrading to PHP 8.4 challenges with Drupal contrib modules
The upgrade from PHP 8.3.14 to PHP 8.4.4 presents challenges for Drupal 10.4 websites, particularly when dealing with contributed modules. While Drupal core operates seamlessly, various contrib modules have not yet been updated to accommodate changes introduced in PHP 8.4.x. This has resulted in...