Using the following code we can post a node trough rest API. However, if there is an image attached to this article we need to post the image node first and then include the image fid in the article.I successfully posted the image also but as far as the documentation is very incomplete I am not able to find a way to add the image fid to the second request to attach it to the article.
$serialized_entity = json_encode([ 'title' => [['value' => $node->title]], 'body' => $node->body_array, 'type' => [['target_id' => $node->node_type]], '_links' => ['type' => [ 'href' => $base_url.'/rest/type/node/'. $node->node_type ]], ]); $response = \Drupal::httpClient() ->post($base_url.'/entity/node?_format=hal_json', [ 'body' => $serialized_entity, 'headers' => [ 'Content-Type' => 'application/hal+json', 'Authorization' => 'Bearer '. $token->access_token ] ]);
Is this the right way to attach an image? if yes, what are the request parameters we need to add the image to the node post request?
Any help would be appreciated. Thanks