How to load an image into a collection entry when creating it?

How to load an image into a collection entry when creating it? I’ve tried several ways but all of them just load the image into “MediaLibrary” but don’t add the image to the entry.

  <!-- Can be multiple files if you setup "collection" instead of "model" -->
  <input type="text" name="name" />
  <input type="file" name="cover" />
  <input type="submit" value="Submit" />

<script type="text/javascript">
  const formElement = document.querySelector('form');

  formElement.addEventListener('submit', e => {

    const request = new XMLHttpRequest();

    const formData = new FormData();

    const formElements = formElement.elements;

    const data = {};

    for (let i = 0; i < formElements.length; i++) {
      const currentElement = formElements[i];
      if (!['submit', 'file'].includes(currentElement.type)) {
        data[] = currentElement.value;
      } else if (currentElement.type === 'file') {
        if (currentElement.files.length === 1) {
          const file = currentElement.files[0];
          formData.append(`files.${}`, file,;
        } else {
          for (let i = 0; i < currentElement.files.length; i++) {
            const file = currentElement.files[i];

            formData.append(`files.${}`, file,;

    formData.append('data', JSON.stringify(data));'POST', `${HOST}/restaurants`);


And if you assign an image to a entry through the admin panel, the image will still not be issued in the get request.
If you really understand this, can you give an example of how to properly create a collection entry with images?

I have a similar scenario where my UI allows images to be uploaded as part of a collection creation/update.

First I upload the Image(s) using a similar REST call to what you use. The response is an array with Image IDs - so I extract the image IDs from the response ready for the next call.

For the next call, I pass the Image IDs to the form data.

Hope it helps