How to get all images of a post in REST API WordPress?

When we add an image to the article body or as a featured image, it does not show up on WordPress REST API by default.

If we have a featured image, we get only the image ID as the default REST API response. If we need to expose all the images in a separate field we can easily do this by modifying the REST API response.

Get all the images of a post:

function add_post_images_rest_api($response, $post, $request) {
    if (isset($post)) {
        $args = array(
            'post_parent'    => $post->ID,
            'post_type'      => 'attachment',
            'numberposts'    => -1, // show all
            'post_status'    => 'any',
            'post_mime_type' => 'image',
            'orderby'        => 'menu_order',
            'order'           => 'ASC'
        );

        $images = get_posts($args); // get all the post images
        $response->data['postImages'] = $images;
    }
    return $response;
}
add_filter('rest_prepare_post', 'add_post_images_rest_api', 10, 3);
Reference: