1

UPDATE 2:

Wow... Seems that if I manually request an OAuth token and then copy/paste that into an Authorization: Bearer ... header manually, my POST request works!!!

Why the built-in Postman OAuth2.0 functionality does not work, for the specific Request, is beyond me??!!!

UPDATE:

Here are the errors that show up when I turn on database logging...

OAuth Error ??? OAuth DBLogging Error

Invalid Type ??? Invalid Type

My Environment

  • "Out of the box" Contenta CMS Distribution
  • New user, consumer, and role created with all "create and view" permissions enabled.
  • Oauth token request working as expected
  • All CORS setup and configured correctly and tested with JSON_API view subrequests

I can't understand why the following basic page post request is failing:

Here's the error:

{
    "errors": [
        {
            "title": "Bad Request",
            "status": 400,
            "detail": "Invalid type specified for related resource: 'node_type--node_type'",
            "links": {
                "info": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1"
            },
            "code": 0
        }
    ]
}

Here's the body I'm sending:

{
 "data": {
  "type": "node--page",
  "attributes": {
    "title": "Hello Page",
    "body": {
        "value": "Custom value",
        "format": "plain_text"
      }
  },
  "relationships": {
    "type": {
      "data": {
        "type": "node_type--node_type",
        "id": "page"
      }
    },
    "uid": {
      "data": {
        "type": "user--user",
        "id": "1"
      }
    }
  }
 }
}

Based off the youtube video here: https://www.youtube.com/watch?v=vEfH2TZAxDg&index=14&list=PLZOQ_ZMpYrZsyO-3IstImK1okrpfAjuMZ

The documentation here doesn't seem to really help too much with the Contenta basic page as the post body object, provided by the API, is significantly different from the example.

https://www.drupal.org/docs/8/modules/json-api/creating-new-resources-post

Any strategies for translating the API provided post to the actual request that needs to be sent is greatly appreciated.


Based off the Contenta API, the correct post url is: /api/pages

Contenta API - Post Basic Page

Below is a screen capture gif showing all of the required fields as defined on the API Screen

Basic Page - Contenta API - Required Fields

Here is my configuration for the consumer, role, user, and permissions

Consuer, Role, user, and permissions

Here is my Post initiated from PostMan. Note the OAuth Token appears to be returned and appended to the header as expected.

Postman Post


PostMan Exported JS

var data = "{\n \"data\": {\n  \"type\": \"node--page\",\n  \"attributes\": {\n    \"title\": \"Hello Page\",\n    \"body\": {\n        \"value\": \"Custom value\",\n        \"format\": \"plain_text\"\n      }\n  },\n  \"relationships\": {\n    \"type\": {\n      \"data\": {\n        \"type\": \"node_type--node_type\",\n        \"id\": \"page\"\n      }\n    },\n    \"uid\": {\n      \"data\": {\n        \"type\": \"user--user\",\n        \"id\": \"1\"\n      }\n    }\n  }\n }\n}";

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("POST", "http://contenta.loc/api/pages?XDEBUG_SESSION_START=PHPSTORM&_format=api_json");
xhr.setRequestHeader("Content-Type", "application/vnd.api+json");
xhr.setRequestHeader("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjViM2RmNmU5NDViYWIzMjFmOWJiMGYyZGM4MzE4N2MxYmFjN2FmZGFkMTA1NTdlMDkzMzEyMDM4NmM1ZTUyOWExYzRmZjY3YjRkY2VmYzZhIn0.eyJhdWQiOiI3N2U0MDUwNi00YjJhLTQzMTctYjZjMC01ZWQ1YjI3Y2U4ODYiLCJqdGkiOiI1YjNkZjZlOTQ1YmFiMzIxZjliYjBmMmRjODMxODdjMWJhYzdhZmRhZDEwNTU3ZTA5MzMxMjAzODZjNWU1MjlhMWM0ZmY2N2I0ZGNlZmM2YSIsImlhdCI6MTUxMzEwMjQwMSwibmJmIjoxNTEzMTAyNDAxLCJleHAiOjE1MTMxMDI3MDEsInN1YiI6IjIxIiwic2NvcGVzIjpbImF1dGhlbnRpY2F0ZWQiLCJjb25zdW1lcl8yXzBfcm9sZSJdfQ.KGbWtYfFwUceVViu1hEEkSZFYiIO1-AznDKaYfzUqgMlwfMQFL1AOeALMrNgtZmSHtKLnPCSp0wrwBQlL7OcJ2DAy8IJOKgCmPv3BDEMkbVvPIY8ho8qeRLkuDIJQFmzBSFxkjb5KajAZm81CdWC13jeQo25E5S1CePM4i1zWL6vNYJJaI9WO0OtMELrTwcFq-gNr-p0bvfrna0JJLPmNVVLCmTj8z34Du_LjfM9oivu62vo3w3PLfpB_UpjOms1eOZ97FV1avtmOjM7ojG7HbNQ4VB5f6fb0XDQUL9QpSWCKwOskNV6nfR_hzoDb2g9dSdE257TZh3RGKbNKurmhOwv2EFnxzLlBCnwMjg72q8PnUhmGkKVO_8g5tui2nj_QDXzACiL-u6YCj4ZMvwGTmRTudJ_Bb0YnQnDjI8vnjMzEWTyLctXQxAKQBbmZxBRdCGrCF0vwFXS8iVNmdfeBmoneKbfvbZpzdvxQ19Q51BaobfNcN0nBcghs1VsbG17OfZmP1OufxEzcFF1ySuWLYZFR1JcH-Fmz-AVs9qHSkyJuKc8T5Yr3vKFwhiTkQ3XwiF4lSpOqChJqoIIA2rHO8Tz4UQ0M-3ae3bCw67c3pTh0t-9JndCaVXt-UTrZqTKQcCufjSq6rrcx5_5yegx-8ulAT6zIuXyytdllbmsA5M");
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.setRequestHeader("Postman-Token", "d596f540-f26d-f933-5ed8-0a0453f99d13");

xhr.send(data);
  • Just a guess, I don't really know Contenta, but "body" is no plain text field in Drupal Core. – Hudri Dec 16 '17 at 9:04
1

A couple things worth checking:

  1. In your PostMan request- note the url has "pages", and not "page". You'll need to confirm the machine name of the content type and see what the /node/add/[node-type] path is when adding content through Drupal's administration backend. Your API request should have the same [node-type] url parameter.

Assuming the url is correct,

  1. Have you confirmed that user 1/admin role has permissions to create new "Page" nodes (from People > Permissions)?
  2. Are there any required fields on the node type that you need to set/pass data for in your JSON?
  3. Turn on error logging and check the Watchdog log upon making a request to post a new node, as it may provide hints as to what is missing/failing.

In the JSON data you're sending, you don't necessarily need to set a relationships.type (even though the tutorial video shows it), as it will inherit that from the node type you specify in data.type.

  • I updated my question above with some additional information based on your suggestions. I'm assuming I don't need to send a path (URL alias) for the same reason you mentioned - "as it will inherit that from the node type". However, if I remove the relationship type, the error changes to: "Invalid type specified for related resource: 'user--user'", so it's choking on the next relationshiop – Justin Levi Winter Dec 18 '17 at 15:58
  • Based on the database logging I added above, it makes me think that oAuth is not correctly processing the Bearer token even though I'm passing it in the header and able to request it fine?? Could this be an oAuth issue??? – Justin Levi Winter Dec 18 '17 at 15:59
  • Giving you credit as your suggestion to check the database logging set me down the path to figure this out. – Justin Levi Winter Dec 18 '17 at 16:40
  • Was it in fact an authentication issue? – Anson W Han Dec 18 '17 at 16:45
  • Indeed - it seems like it was a Postman issue with the built-in Authorization functionality versus copy/pasting the token manually. Super frustrating - I don't even want to admit how much time I wasted on this issue. – Justin Levi Winter Dec 24 '17 at 16:28

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.