How to receive the file sent from curl in an express file?

Viewed 22

I want to accept the file sent in the request object from the curl CLI and modify it in my express.js server.

For example: User attempts as such:

curl -T somefile.txt https://example.com/uploadfile

Accept it in:

app.put("/uploadfile", function (req, res) {
  // access the file here
});
1 Answers

curl -T somefile.txt https://example.com/uploadfile produces the following request:

PUT /uploadfile/somefile.txt HTTP/1.1
Host: example.com
Accept: */*
Content-Length: ...
Expect: 100-continue

<contents of the .txt file>

Therefore you need a middleware like

app.put("/uploadfile/:filename", function(req, res) {
  /* Access the the file as the readable stream req, for example: */
  req.pipe(process.stdout);
  res.end();
});

Node.js handles the Expect header automatically, see here.

Related