Error POSTing a comment using JSON

Created on 8 September 2023, 9 months ago
Updated 26 May 2024, 21 days ago

Problem/Motivation

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

I am unable to use standard article with standard comments (machine name of comment field on on article is "comment"):

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? Which type is missing?!
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

Postponed: needs info

Version

11.0 🔥

Component
JSON API  →

Last updated 2 days ago

Created by

🇦🇹Austria maxilein

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024