1

I am sending FormData() to a cloud function with something like this in the JavaScript frontend.

//Frontend Angular
const formData = mew FormData();
formData.append('file1', zipFile1);
formData.append('file2', zipFile2);
formData.append('name', 'MyFiles');

this.http.post(urlAPI, formData) //angularJS


//Cloud Function
function main(param) {
   console.log(param);
}

The console log on the param shows the content-type is multipart/form-data is in the header. There is also a property labeled as __ow_body. This value is a very long string of characters and digits. I am unsure what this is but I am assuming it is the files I am sending in stream/serialized format.

{
  __ow_body: 'hf381fh891hv831h93n19384v938v892vn98vn2890vn29n9vn9892vn948vn2893vn2985hv98...'
}

I wanted to confirm if this is the stream data, and if so, how can I parse this?

I need to send this file, which is a zip file containing images, to a API I am using. In this documents API, the examples show sending a file in the local file system such as

someApiFunction('./myImgs.zip').then(...);

The problem lies in me sending my zip file over http network protocol and the format is very different I think from the example of reading a file in the local file system/machine. How can I deserialize/ or parse my file so that it can be recognized as a zip file containing images inside?

I tried using fs.createReadStream but it doesn't seem to be parsing it. It will just make a more structured object, but within that object, I don't see my formData and its keys such as file1 file2 and name.

Do I need to write this file some where first?

henhen
  • 1,038
  • 3
  • 18
  • 36
  • when making JSON from `formData` it will become an empty object ... `{}` - combining formData and JSON seem an odd thing to do in a request – Bravo Oct 08 '19 at 02:32
  • so how can I send it if I do not make it a JSON? – henhen Oct 08 '19 at 03:36

1 Answers1

0

Even if it is less probable, you can try to change a bit the way of how you are posting the formData. I have found a really nice documentation about how to use formData with Angular. As it is stated there you can try to post is using something like this :

  this.httpClient.post<any>(this.SERVER_URL, formData).subscribe(
  (res) => console.log(res),
  (err) => console.log(err)
); 

This is less probable to work, but you may give it a shot.

The thing which is certain is that because of the way Cloud Functions pre-processes some requests, you can expect that some libraries not to work and I think this is the exact situation for you.

As it is stated in the Cloud Functions documentation , you need to use rawBody property of the request and 'the bus-boy' library in order to process multipart/form-data.

Here is one of the code example for handling this kind of requests.

Andrei Tigau
  • 2,010
  • 1
  • 6
  • 17