How to attach a file in nodejs /node-fetch POST request.I want to invoke an API which import a file (.xls or csv). How can I do this using Node-fetch / nodejs.
相关问题
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- google-drive can't get push notifications
- How to reimport module with ES6 import
- Why is `node.js` dying when called from inside pyt
- How to verify laravel passport api token in node /
相关文章
- node连接远程oracle报错
- How can make folder with Firebase Cloud Functions
- @angular-cli install fails with deprecated request
- node.js modify file data stream?
- How to resolve hostname to an ip address in node j
- Transactionally writing files in Node.js
- Log to node console or debug during webpack build
- Get file created date in node
This is a express server that streams a local file to the response.
README.md
says:And sources indicate it supports several types, like
Stream
,Buffer
,Blob
... and also will try to coerce asString
for other types.Below snippet shows 3 examples, all work, with v1.7.1 or 2.0.0-alpha5 (see also other example further down with
FormData
):Here is
foo.txt
:Note:
http://httpbin.org/post
replies with JSON that contains details on request that was sent.Result:
If you need to send a file as part of a form with more parameters, you can try:
npm install form-data
FormData
object as body (FormData
is a kind ofStream
, viaCombinedStream
library)header
in the options (unlike examples above)and then this works:
Result (just showing what is sent):
There is one thing to add to Hugues M.'s answer. File upload with
FormData
is not supported bynode-fetch
. Instead, you need to use other libraries such asrequest
orrequest-promise
. Below is a code snippet for uploading a file usingrequest-promise
library.resolveWithFullResponse
option gives you the raw response. Without this, the promise returns just the body of the response.