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:
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.
I can't find any documentation on making post requests to Drupal 8 REST so I am assuming this is an issue with core.
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | drupal-8-post-article-node-rest-token.png | 197.09 KB | askibinski |
| pic2.png | 92.34 KB | epiro01 | |
| pic1.png | 57.15 KB | epiro01 |
Comments
Comment #1
askibinski commentedShouldn'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
Comment #2
dawehnerYeah absolutely right, the route for creating entities is
/entity/nodebut I can't agree, ideally the default frontpage should be/nodesComment #3
askibinski commentedFound some useful documentation here:
https://www.drupal.org/node/2405657
Comment #4
askibinski commentedAllright, 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.
Comment #5
AndyLicht commentedHi,
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
Comment #6
tyler.frankenstein commented@AndyLickt, try changing your type to:
"type":[{"target_id":"article"}]Comment #7
extexan commentedI'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.
Comment #8
extexan commentedAhh, my bad. I had omitted the "http://" from the "type" URL. Ignore my previous post.
Comment #9
pranil_kochar commentedDoes 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
Comment #10
damien_th commentedHere is my working code, hope this help.
Prerequisites:
Here is my Query:
POST your.drupal.website/entity/user?_format=hal_jsonHeaders:
Body: