Drupal 通过 post Api 调用创建节点失败并显示消息 "Could not determine entity type bundle: \u0022type\u0022 field is missing."}

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

我正在尝试通过 drupal 创建节点 API 但出现此错误:

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."}

这是我的职能:

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; 
     }
       

但我真的不奇怪,因为这个其他功能正在工作

 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; 
}

有人能理解我的错误吗?

这是因为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 ] ],
        //]
    ])
];