0

I've figured out the basics of how to use a simple POST request to create a new Drupal 8 node. For example, using the JSON:API (https://www.drupal.org/docs/8/modules/jsonapi/creating-new-resources-post#basic-post-request) with a payload that looks like the following.

{
  "data": {
    "type": "node--article",
    "attributes": {
      "title": "My custom title",
      "body": {
        "value": "Custom value",
        "format": "plain_text"
      }
    }
  }
}

However, I have no control over the payload, and I need to create a new node from a JSON post request that looks like the following.

{
 "title": "My title",
 "message: "My Message",
}

The documentation and available examples for accepting POST requests I'm finding so far are few and far between, so just wondering if anybody has any guidance or examples to share.

I'm assuming we will need to implement a custom module of some sort to serialize/denormalize the payload, but the more I read the more I'm unclear about how to approach this or what exactly we need to implement. It has been a long day, and I'm just looking for the simplest approach to accepting a POST request like this to create a simple node.

-1

I am sharing the reference link from drupal, please take look https://www.drupal.org/docs/8/api/restful-web-services-api/custom-rest-resources

Instead of get() function use post($request). Eg:

public function post($request) { $data = (array) $request; }

You will get all the data sent in json format in $data variable. After that write a piece code to create an entity(node/taxonomy/user).

  • The OP is asking about the JSON data to send. He well know to send POST requests, as the question says. – kiamlaluno Aug 13 at 6:01
  • From the above, he is having request like: { "title": "My title", "message: "My Message", } he just need to get the request in array. Eg: $data['title'] which he will receive in post. – Ravi singh Aug 13 at 6:19
  • The OP is using JSON:API and sending a HTTP POST request. Telling the OP to make an HTTP POST request, which the OP is already doing, or to use post() instead of get() doesn't help him. – kiamlaluno Aug 13 at 7:53
  • 1
    Hi @Ravisingh thanks for the response, I actually think the link you shared may be a path forward if it works as you suggest. I don't care if I use JSON:API or REST or something custom, I just want to be able to easily create nodes from a POST request that is formatted like my example above. I'm going to give that a shot and will let you know how it goes. In the meantime I'd still be interested in any other responses or examples. There are millions of examples for using Drupal to read data via API but very few that show complex ways of using Drupal as a consumer. – Tim Broeker Aug 13 at 23:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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