Score:0

Create node by post Api call fail with message "Could not determine entity type bundle: \\u0022type\\u0022 field is missing."}

cn flag

I'm trying to create a node via Drupal API but I get this error:

Got error 'PHP message: PHP Fatal error:  Uncaught GuzzleHttp\\Exception\\ClientException: Client error: `POST https://site.it/entity/node?_format=hal_json` resulted in a `422 Unprocessable Entity` response:\n{"message":"Could not determine entity type bundle: \\u0022type\\u0022 field is missing."}

This is my function:

public function createFaq($notes, $telegram_id){
  $url = "/entity/node?_format=hal_json"; 
  $opt = [
    'headers' => self::$baseHeader,
    'body' => json_encode([
      [
        'type' => [ ['target_id' => 'faq'] ],
        'title' => 'title', 
        'utente' => [ [ 'target_id' => '123462' ] ],
        'field_domanda' => [ [ 'value' => $notes['domanda'] ] ],
        'field_presenza' => [ [ 'value' => $notes['presenza'] == "Si"? true : false ] ],
      ]
    ])
  ];

  $response = $this->client->request('POST', $url , $opt);
  $r = json_decode( $response->getBody());
  return $r; 
}

But it's really strange, because this other function is working:

public static function createUser($title){
  $url= "/entity/node?_format=hal_json"; 
  $opt = [
    'headers' => self::$baseHeader,
    'body' => json_encode([
      'title' => [ [ 'value' => $title ] ],
      'type' => [ [ 'target_id' => 'article' ] ],
    ])
  ];

  $response = $this->client->request('POST', $url , $opt);
  $r = json_decode( $response->getBody());
  return $r; 
}

Can someone understood my error?

berliner avatar
bd flag
Your second (working) example has this POST body `['type' => ...]` whereas the first example has it wrapped in an additional array: `[['type' => ...]]`.
Shyghar avatar
cn flag
You are right!! Thank you <3
Score:0
cn flag

This is the code I used to create a node using rest api:

Endpoint: /node?_format=hal_json

Method: POST

Body:

{
  "title": [
    {
      "value": "This is a question"
    }
  ],
  "field_question": [
    {
      "value": "This is the question"
    }
  ],
  "field_options": [
    {
      "value": "This is the right option"
    },
    {
      "value": "This is not the right option"
    },
    {
      "value": "This is defenitely not the right option"
    },
    {
      "value": "This is the most silly option"
    }
  ],
  "type": [
    {
      "target_id": "question"
    }
  ]
}

Enable the Rest UI module to check if the endpoint exists. Actually I can't find the plugin proposing this endpoint but having Rest UI enabled I saw this URI for posting content types.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.