Example on how to POST a comment using JSON

Created on 8 September 2023, over 1 year ago

Problem/Motivation

Could someone please show a working example of posting a comment to a node.

E.g. article with standard comments:

GET for https://my.example.site/jsonapi/comment/comment returns:

{
    "jsonapi": {
        "version": "1.0",
        "meta": {
            "links": {
                "self": {
                    "href": "http://jsonapi.org/format/1.0/"
                }
            }
        }
    },
    "data": [
        {
            "type": "comment--comment",
            "id": "bdb67ec2-cac4-48b6-898d-c9aac5578e51",
            "links": {
                "self": {
                    "href": "https://my.example.site/jsonapi/comment/comment/bdb67ec2-cac4-48b6-898d-c9aac5578e51"
                }
            },
            "attributes": {
                "drupal_internal__cid": 3,
                "langcode": "en",
                "status": true,
                "subject": "Test Nr 2 Commenbt",
                "name": null,
                "mail": null,
                "homepage": null,
                "created": "2023-09-08T15:33:34+00:00",
                "changed": "2023-09-08T15:33:34+00:00",
                "thread": "01/",
                "entity_type": "node",
                "field_name": "comment",
                "default_langcode": true,
                "comment_body": {
                    "value": "<p>Boddy</p>",
                    "format": "basic_html",
                    "processed": "<p>Boddy</p>"
                }
            },
            "relationships": {
                "comment_type": {
                    "data": {
                        "type": "comment_type--comment_type",
                        "id": "18ffd67d-d544-40f7-bfde-c7a09466f864",
                        "meta": {
                            "drupal_internal__target_id": "comment"
                        }
                    },
                    "links": {
                        "related": {
                            "href": "https://my.example.site/jsonapi/comment/comment/bdb67ec2-cac4-48b6-898d-c9aac5578e51/comment_type"
                        },
                        "self": {
                            "href": "https://my.example.site/jsonapi/comment/comment/bdb67ec2-cac4-48b6-898d-c9aac5578e51/relationships/comment_type"
                        }
                    }
                },
                "uid": {
                    "data": {
                        "type": "user--user",
                        "id": "3725e823-7afb-41cc-a180-2f8dcd381edb",
                        "meta": {
                            "drupal_internal__target_id": 1
                        }
                    },
                    "links": {
                        "related": {
                            "href": "https://my.example.site/jsonapi/comment/comment/bdb67ec2-cac4-48b6-898d-c9aac5578e51/uid"
                        },
                        "self": {
                            "href": "https://my.example.site/jsonapi/comment/comment/bdb67ec2-cac4-48b6-898d-c9aac5578e51/relationships/uid"
                        }
                    }
                },
                "pid": {
                    "data": null,
                    "links": {
                        "related": {
                            "href": "https://my.example.site/jsonapi/comment/comment/bdb67ec2-cac4-48b6-898d-c9aac5578e51/pid"
                        },
                        "self": {
                            "href": "https://my.example.site/jsonapi/comment/comment/bdb67ec2-cac4-48b6-898d-c9aac5578e51/relationships/pid"
                        }
                    }
                },
                "entity_id": {
                    "data": {
                        "type": "node--article",
                        "id": "b66ef53d-5f55-4e97-b5c4-85c46eb2fe22",
                        "meta": {
                            "drupal_internal__target_id": 5861
                        }
                    },
                    "links": {
                        "related": {
                            "href": "https://my.example.site/jsonapi/comment/comment/bdb67ec2-cac4-48b6-898d-c9aac5578e51/entity_id"
                        },
                        "self": {
                            "href": "https://my.example.site/jsonapi/comment/comment/bdb67ec2-cac4-48b6-898d-c9aac5578e51/relationships/entity_id"
                        }
                    }
                }
            }
        }
    ],
    "links": {
        "self": {
            "href": "https://my.example.site/jsonapi/comment/comment"
        }
    }
}

When I try to POST a comment to an article I get an error:

"errors": [
{
"title": "Bad Request",
"status": "400",
"detail": "Resource object must include a \"type\".",

Here is the POST

{
    "data": [
        {
            "type": "comment--comment",
            "attributes": { 
                "langcode": "en",
                "status": true,
                "subject": "Test Nr 3 Comment",
                "entity_type": "node",
                "field_name": "comment",
                "default_langcode": true,
                "comment_body": {
                    "value": "<p>Boddy</p>",
                    "format": "basic_html",
                    "processed": "<p>Boddy</p>"
                }
            },
            "relationships": {
                "comment_type": {
                    "data": {
                        "type": "comment_type--comment_type",
                        "id": "18ffd67d-d544-40f7-bfde-c7a09466f864",
                        "meta": {
                            "drupal_internal__target_id": "comment"
                        }
                    },
                    "links": {
                        "related": {
                            "href": "https://my.example.site/jsonapi/comment/comment/bdb67ec2-cac4-48b6-898d-c9aac5578e51/comment_type"
                        },
                        "self": {
                            "href": "https://my.example.site/jsonapi/comment/comment/bdb67ec2-cac4-48b6-898d-c9aac5578e51/relationships/comment_type"
                        }
                    }
                },
                "uid": {
                    "data": {
                        "type": "user--user",
                        "id": "3725e823-7afb-41cc-a180-2f8dcd381edb"
                    }
                },
                "entity_id": {
                    "data": {
                        "type": "node--article",
                        "id": "b66ef53d-5f55-4e97-b5c4-85c46eb2fe22",
                        "meta": {
                            "drupal_internal__target_id": 5861
                        }
                    },
                    "links": {
                        "related": {
                            "href": "https://my.example.site/jsonapi/comment/comment/bdb67ec2-cac4-48b6-898d-c9aac5578e51/entity_id"
                        },
                        "self": {
                            "href": "https://my.example.site/jsonapi/comment/comment/bdb67ec2-cac4-48b6-898d-c9aac5578e51/relationships/entity_id"
                        }
                    }
                }
            }
        }
    ],
    "links": {
        "self": {
            "href": "https://my.example.site/jsonapi/comment/comment"
        }
    }
}

Can someone please help me with an explanation?
What am I doing wrong?

I could really nowhere find an example for posting a comment using JSON.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

💬 Support request
Status

Active

Version

10.1

Component
JSON API 

Last updated 3 days ago

Created by

🇦🇹Austria maxilein

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024