Home > Software design >  Drupal create node by post Api call fail with message "Could not determine entity type bundle:
Drupal create node by post Api call fail with message "Could not determine entity type bundle:

Time:01-12

I'm trying to create a node via drupal API but I have 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 i't 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?

CodePudding user response:

This is because the json data are enclosed in square brackets twice, just remove one pair :

$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 ] ],
        //]
    ])
];
  •  Tags:  
  • Related