Deno Oak: How to send an object with many blobs in only one response to the Web Browser

357 Views Asked by At

I am using Deno, with the Oak framework. I want to send a FormData object as a response from the server, because I want to send (in only one response, instead of making many calls) an object which contains many blobs (for example, a group of images), so it can be read in the client, the web browser.

My backend code is:

  // Create a new instance of FormData
  const formData = new FormData();

  // Add some key-value pairs to the FormData object
  formData.append("key1", "value1");
  formData.append("key2", "value2");

  // Send the FormData object as the response to the client
  context.response.body = formData;

In the client, to receive it (and previously making the call), I have this code, but here I encounter the problem:

await fetch(`/myroute/get`, { 
    method: 'POST',
    body: myFormData, // this is another FormData, that I sent from the client and read in the server with no problems.
  })
  .then(fetch => fetch.formData()) // Error!
  .then(res => { console.log(res); }); 

The problem is, that if I try to read the response with fetch.formData(), I get:

Uncaught (in promise) TypeError: Could not parse content as FormData.

I have tried with Firefox and Chrome, and the error (to my understanding) seems to be that the "boundary" of the FormData is not properly setted.

So my question is, does anybody how to solve this problem, or has any functional code to do that task? (that is, to send many blobs from Oak, in only one response and receive it correctly). I don't know If that can be accomplished using something different than FormData, in which case I am also interested. Many thanks in advance.

1

There are 1 best solutions below