简体   繁体   中英

How to add files in a POST using request-promise of npm?

I'm tryng to send a Curl request using "request-promise" of npm. The Curl that I want to send is as follows:

`curl \
-H "Content-Type: multipart/form-data" \
-F "original=@./${parent_path}"  \
-F "modified=@./${version_path}" \
-o "${out_path}" \
 ${URI}`

My code in node is:

BIMFile.findOne({ _id: responseDB.parent_id })
  .then(parent => {
      parent_path = parsePath(parent.path);
      version_path = parsePath(responseDB.path);
      console.log("PARENT!", parent_path, version_path);
      const URI =
        `${protocol}://${host_img_diff}:${port_img_diff}/diff`
      out_path = version_path + '.tmp.jpg';

      request.post({
        url: URI,
        formData: {
          file: fs.createReadStream(parent_path),
          file: fs.createReadStream(version_path)
        }
      }).then((apiResponse) => {
        console.log('apiUPDATEResponse', apiResponse);
      })

The result is:

Unhandled rejection StatusCodeError: 400 - "<!DOCTYPE HTML PUBLIC 
\"-//W3C//DTD HTML 3.2 Final//EN\">\n<title>400 Bad 
Request</title>\n<h1>Bad Request</h1>\n<p>The browser (or proxy) sent a re
quest that this server could not understand.</p>\n"
at new StatusCodeError (/backend/node_modules/request-promise- 
core/lib/errors.js:32:15)
at Request.plumbing.callback (/backend/node_modules/request-promise- 
core/lib/plumbing.js:104:33)
at Request.RP$callback [as _callback] (/backend/node_modules/request- 
promise-core/lib/plumbing.js:46:31)
at Request.self.callback (/backend/node_modules/request/request.js:185:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request.<anonymous> (/backend/node_modules/request/request.js:1161:10)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
at IncomingMessage.<anonymous> 
(/backend/node_modules/request/request.js:1083:12)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1055:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)

The server returns the following message:

xxx.xx.x.xx - - [05/Apr/2019 10:00:15] "POST /diff HTTP/1.1" 400 -

As you can see the server couldn't understand the post request. Does anyone know how to add files correctly?

Use send files using formData as you're doing, but one of the issues you have is that you're setting both files in the same property, and only the last one is set.

 console.log({ file: 1, file: 2 }); 

So if file can receive multiple files, you need to use an array

const formData = {
   file: [
       fs.createReadStream(parent_path),
       fs.createReadStream(version_path)
   ]
}

If you need additional meta-data, request module provides a way too:

Pass optional meta-data with an 'options' object with style: {value: DATA, options: OPTIONS} Use case: for some types of streams, you'll need to provide "file"-related information manually.
See the form-data README for more information about options: https://github.com/form-data/form-data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM