I have a Drupal 8.3 setup and working and want to add content via REST. I can create nodes fine, but only in plain text. I can't figure out and can't find any documentation for how to submit content as html so it could have formatting.

I found a comment that had examples for submitting comments with basic html with the body specified like:

{"content_body": {"format": "basic_html", "value": "<b>Hi</b>"}

But when I tried to do something similar with the 'body' tag for posting to /entity/node, it returns a 422 response with this message:

Unprocessable Entity: validation failed.\nbody: Body: this field cannot hold more than 1 values

For reference, this is basically the hal+json body of the post (minus the taxonomy stuff not relevant to this question):

{
  "body": {
    "format": "basic_html", 
    "value": "<b>This is the body of the test</b>"
  }, 
  "title": {"value": "Test One"}, 
  "type": {"target_id": "Article"}
}

If I drop the 'format' tag from the body dictionary, everything works fine... except the tags are displayed and not interpreted as html.

Thanks in advance.

Drupal is probably expecting an array of field data, even if the field itself only accepts a single value.

When the JSON is decoded, this is still an array:

{
  "format": "basic_html", 
  "value": "<b>This is the body of the test</b>"
},

But with 2 items, when only one is expected.

You should be able to fix by providing an array with just the one item:

"body": [{
  "format": "basic_html", 
  "value": "<b>This is the body of the test</b>"
}],

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.