Last updated March 24, 2014. Created by linclark on September 26, 2013.
Edited by klausi. Log in to edit this page.

Currently, the only core format which can handle POST requests is application/hal+json. Enable HAL module to support this media type. In the following examples we use HTTP Basic Authentication to authenticate the request.

Configuration

Follow the steps in the GET configuration instructions. To enable POSTing to a resource, you must add it to the nested array for the resource. For example, the following config enables both GET and POST on nodes.

# Example configuration for enabling REST resources.
resources:
  # Enable the node resource.
  'entity:node':
    GET:
      supported_formats:
        - json
        - hal_json
      supported_auth:
        - basic_auth
    POST:
      supported_formats:
        - hal_json
      supported_auth:
        - basic_auth

Setting permissions

Permissions are configured using the same steps as for GET requests. Typically, a limited number of roles will be given access to POST content entities.

Testing with a POST request

To follow these tests, grant access to the role which you are testing with. To ensure that authentication is working, you should not grant access to the anonymous user until you have verified that it is working.

cURL (command line)

curl --include --request POST --user klausi:secret \
--header 'Content-type: application/hal+json' \
http://drupal-8.localhost/entity/node --data-binary \
'{"_links":{"type":{"href":"http://drupal-8.localhost/rest/type/node/page"}}, \
"title":[{"value":"test title"}]}'

Guzzle

<?php
use Guzzle\Http\Client;
$client = new Client('http://drupal-8.localhost');
// If in a Drupal environment use the HTTP client service.
$client = \Drupal::httpClient()->setBaseUrl('http://drupal-8.localhost');
$node = array(
 
'_links' => array(
   
'type' => array(
     
'href' => 'http://drupal-8.localhost/rest/type/node/page'
   
)
  ),
 
'title' => array(0 => array('value' => 'New node title')),
);
$data = json_encode($node);
$response = $client->post('entity/node', array(
   
'Content-type' => 'application/hal+json',
  ),
$data)
 
// Username and password for HTTP Basic Authentication.
 
->setAuth('klausi', 'secret')
  ->
send();
if (
$response->getStatusCode() == 201) {
  print
'Node creation successful!';
}
?>

Dev HTTP client

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

I'm trying to run the post request via command line and I'm getting this error:

HTTP/1.1 400 Bad Request
Date: Thu, 17 Oct 2013 14:34:40 GMT
Server: Apache
X-Powered-By: PHP/5.3.20
Cache-Control: must-revalidate, no-cache, post-check=0, pre-check=0, private
X-UA-Compatible: IE=edge,chrome=1
Content-language: en
Last-Modified: Thu, 17 Oct 2013 14:34:40 GMT
ETag: "1382020480"
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Connection: close
Transfer-Encoding: chunked
Content-Type: application/hal+json

{"error":"The type link relation must be specified."}

try this instead :
curl --include --request POST --user admin:admin --header 'Content-type: application/hal+json' http://localhost/drupal8/entity/node/ --data-binary '{"_links":{"type":{"href":"http://localhost/drupal8/rest/type/node/page"}},"title":[{"value":"test title"}]}'

Formateur, consultant, Drupal 6,7,8, symfony2