meteor multiform http post request
Asked Answered
K

0

7

I am pretty new to Web Dev, and Meteor, and all things REST, but I am trying to write a server side method to make a Meteor http post request to a third party server, and upload an image to it. I am having trouble setting it up correctly. I want to upload a file in the body of a multipart/form-data section, but I am having trouble generating the correct request...

This is what i have:

Meteor.methods({
    postOCR:function(newFile){

        var options = {
        headers: {'secret': mySecret,
                  'Content-Type': 'multipart/form-data'},
        data: {'Content-Disposition': 'form-data',
               'name':'image',
               'filename':newFile
        }
    }

    HTTP.call('POST', url, options, function(error, result) {
    if (error) {
      console.log('ERRR');
      console.log(error);
    } else
      console.log('RESULT');
      console.log(result);
  });
}
});

And this is the request I am trying to build:

POST /some/res HTTP/1.1
Host: myUrl
secret: mySecret
Cache-Control: no-cache

----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="multipart/form-data"; filename="img.jpg"
Content-Type: image/jpeg
----WebKitFormBoundaryE19zNvXGzXaLvS5C

The initial request is going through fine, but I don't seem to be uploading the file correctly...Can anyone tell me what I am doing wrong?

Thanks!

Kun answered 28/2, 2016 at 3:1 Comment(4)
I think you can try using the npm request package to send a file more easily. Also checkout froatsnook:request which wraps around the request package.Hoffer
I'm assuming you are not using any particular packages or libraries to upload your file. If you are open to use a third party like AWS (amazon web services) you can use a solid package like slingshot to handle the file uploading. This will also free your main app from any static files (best practices) Slingshot tutorialNickerson
@Kun have you got the solution?Mudslinger
check github.com/rosh93/meteor-multipart-form-dataTypo

© 2022 - 2024 — McMap. All rights reserved.