1

my REST API should send a file to another API and I don't know how to solve it. The only thing i get from the Endpoint API is this curl command:

curl -i -X POST --data-binary @localimg.jpg --header 
"X-API-PersonalKey: XXXX-XXXX-XXXX-XXXX" --header "ContentType:application/octet-stream" 
http://XXX.XX/..../upload

(btw I have no exp with curl or this functions, so I must learn on the fly.)

I searched and found a tutorial with filesystem Readstream and request POST.

My solution is this (and it dosen't work)

      var fs = require('fs');
      var url = "http://XXX.XX/..../upload";
          console.log(url);
      var fdata = fs.createReadStream("c:/xxx/xxx/localimage.jpg");
          console.log(fdata);
      var options = {
                    url: url,
                    headers: {
                        'X-API-PersonalKey': authToken,
                        "Content-Type": "application/octet-stream"
                    },
                    data: fdata,
                    body: fdata

                };
      request.post(options, function optionalCallback(err, httpResponse, body) {
        if (err) {
           return console.error('upload failed:', err);
                 }
         console.log("httpRes =" + httpResponse);
         console.log('Upload successful!  Server responded with:', body);    
                });
Ckappo
  • 607
  • 1
  • 9
  • 27

1 Answers1

0

You need to post multipart/form-data is to use its form feature.

Look at this: https://stackoverflow.com/a/25345124/4645298