I am having issues sending a POST request to create a node with Drupal 8 Core REST. Here are the steps I have went through several times. Note that these steps are in effort to produce a working example, I am not worried about the security implications as of yet.

1. Install latest version of drupal 8
2. Enable all core web services modules (HAL, HTTP Basic Authentication, RESTful Web Services, Serialization)
3. Go to permissions page. Find RESTful Web Services permissions and allow delete, get, patch, and post for all users. Find Node Article permissions, allow create, delete and edit for all users.
4. Launch Dev HTTP Client in Chrome. Input the following request:

http://bonkmas.com/pic1.png

After hitting Send, I receive the following message which I believe should be a 201 and not a 200 if a node were to successfully be created. I think the REST part is working, its just I cant seem to get a POST request to create a node.

http://bonkmas.com/pic2.png

I can't find any documentation on making post requests to Drupal 8 REST so I am assuming this is an issue with core.

Comments

askibinski’s picture

Shouldn't the endpoint be /entity/node instead of /node ? I know this was changed so maybe it is /node but most articles on the web use /entity/node

dawehner’s picture

Yeah absolutely right, the route for creating entities is /entity/node but I can't agree, ideally the default frontpage should be /nodes

askibinski’s picture

Found some useful documentation here:
https://www.drupal.org/node/2405657

askibinski’s picture

Status: Active » Closed (works as designed)
StatusFileSize
new197.09 KB

Allright, got it working returning a nice 201. See attachment.

@epiro01, 2 things:

1. You forget the Accept header
2. You need an authentication set AND CSRF token (which you can request via /rest/session/token)

This is using Drupal 8 beta10.

Closing this as it works as designed, although documentation is often incomplete on this topic.

AndyLicht’s picture

Hi,
thanks for this helping lines, but in Drupal 8 beta 16 i got this error:

400 - Bad Request with this body-message:
{
"error": "A string must be provided as a bundle value."
}

This is my request:
{
"title": [
{
"value": "TESTE NODE"
}
],
"type": [
{
"value": "article"
}
],
"_links": {
"type": {
"href": "http://localhost/d8/type/node/article"
}
}
}

Maybe you have an answer for me.
thanks
Andy

tyler.frankenstein’s picture

@AndyLickt, try changing your type to:

"type":[{"target_id":"article"}]

extexan’s picture

I've tried all the different formats from this thread (and others I've found via Google) - using DHC in Chrome to send the request.

At first I was getting 403 errors, which (I think) got fixed by making sure I got the X-CSRF-Token from within the same browser I was using to send the request.

Now getting "400 Bad Request" error... "Type mysite.com/rest/type/node/page does not correspond to an entity on this site."

I pasted the URL in my browser, and get "Page not found". I've tried rest/type/node/page, rest/type/node/article, and even rest/type/node/my_content_type (one that I have created). All return "Page not found".

I tried it without the "_link" statement, but then it told me that was required.

Any help would be greatly appreciated.

extexan’s picture

Ahh, my bad. I had omitted the "http://" from the "type" URL. Ignore my previous post.

pranil_kochar’s picture

Does anybody know how to create a user using rest api in drupal.
my url is : http://localhost/entity/user
i m facing difficulty in forming a request body to hit this api

damien_th’s picture

Here is my working code, hope this help.

Prerequisites:

  • Rest Services module must be enabled
  • Rest UI module must be installed and enabled
  • Perform this as an administrator or adjust REST Services rights in Extend => Rest Services => Rights
  • In configuration => REST, enable resource "user" for POST with hal+json, and the authentication prefer you prefer. In my example below, it is Basic Authentication.

Here is my Query:
POST your.drupal.website/entity/user?_format=hal_json

Headers:

Content-Type: application/hal+json
X-CSRF-Token: <your_token_retrieved_from_your.drupal.website/rest/session/token>
Authorisation: Basic xxxxxxxxxxxxxxxxxxxxxxx

Body:

{
    "_links": {
      "type": {
        "href": "http://your.drupal.website/rest/type/user/user"
      }
    },
	"name":[{"value":"test"}],
	"mail":[{"value":"test@test.fr"}],
	"roles":[{"target_id":"administrator"}],
	"pass":"testpassword"
}