0

Im working with Drupal 8 and Angular.

I want to create a content from angular.

I have the Get method working.

The problem is with the Post method, I don't know how to configure it.

With the Rest Ui module, i have this configuration:

enter image description here

And in the services.yml file I have this one:

  cors.config:
    enabled: true
    # Specify allowed headers, like 'x-allowed-header'.
    allowedHeaders: []
    # Specify allowed request methods, specify ['*'] to allow all possible ones.
    allowedMethods: []
    # Configure requests allowed from specific origins.
    allowedOrigins: ['*']
    # Sets the Access-Control-Expose-Headers header.
    exposedHeaders: false
    # Sets the Access-Control-Max-Age header.
    maxAge: false
    # Sets the Access-Control-Allow-Credentials header.
    supportsCredentials: false

It works fine for the Get method, but for the POST, it gives me this error:

XMLHttpRequest cannot load http://drupal.dd:8083/entity/node. Preflight response is not successful task.service.ts:92

The task.service.ts is from my Angular app.

Questions:

1) Is the problem from Drupal configuration or from angular? (if is from angular, i will move the post. )

2)Wich on is the correct way to configure Post Method in Drupal?

Extra info from the method enter image description here

  • allowedMethods: [] means no methods are allowed. So the OPTIONS call fails. Add the appropriate methods to that array, clear the cache, it should work – Clive Dec 15 '17 at 12:03
1

This is the right cors.config configuration:

 cors.config:
        enabled: true
        # Specify allowed headers, like 'x-allowed-header'.
        allowedHeaders: ['*']
        # Specify allowed request methods, specify ['*'] to allow all possible ones.
        allowedMethods: ['*']
        # Configure requests allowed from specific origins.
        allowedOrigins: ['*']
| improve this answer | |
-1
resources: 
    entity:node:
        GET: 
            supported_formats: 
                  - hal_json
            supported_auth: 
                  - basic_auth - cookie     
        POST: 
             supported_formats:
                 - hal_json supported_auth:
                - basic_auth - cookie

Curl request

curl --include \ --request POST \ --user klausi:secret \ --header 'Content-type: application/hal+json' \ --header 'X-CSRF-Token: <obtained from http://example.com/rest/session/token>' \ http://example.com/entity/node?_format=hal_json \ --data-binary '{"_links":{"type":{"href":"http://example.com/rest/type/node/article"}},"title":[{"value":"Example node title"}],"type":[{"target_id":"article"}]}'

jQuery Request

function getCsrfToken(callback) { jQuery .get(Drupal.url('rest/session/token')) .done(function (data) { var csrfToken = data; callback(csrfToken); }); } function postNode(csrfToken, node) { jQuery.ajax({ url: 'http://example.com/entity/node?_format=hal_json', method: 'POST', headers: { 'Content-Type': 'application/hal+json', 'X-CSRF-Token': csrfToken }, data: JSON.stringify(node), success: function (node) { console.log(node); } }); } var newNode = { _links: { type: { href: 'http://example.com/rest/type/node/article' } }, type: { target_id: 'article' }, title: { value: 'Example node title' } }; getCsrfToken(function (csrfToken) { postNode(csrfToken, newNode); });

Note: If using Basic Auth You need to set the "Authorization" header Here is an example:

function formatBasicAuth(userName, password) {

var basicAuthCredential = userName + ":" + password; var bace64 = btoa(basicAuthCredential); return 'Basic ' + bace64; }

// then in your post var basic = formatBasicAuth('userName', 'password'); ...

'X-CSRF-Token': csrfToken, 'Authorization': basic,

| improve this answer | |

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.