How do I put my file into request body using fetch

I’m following Camunda Rest API tutorial. I’m trying to create a new process is diagram_1.bpmn.
Here is my code. I am trying to put my file into request body like postman


I have tried many times but it still didn’t work! Although I got my req.file successfully

Can you print the form data? Seems like a missmatch between the fields you post and the request parameters specified here.

Best regards
Sascha

This is my Formdata. Please check it. Thank you very much.

After seeing your more recent post, I’m pretty sure your issue stems from the headers (specifically the boundary). Probably easiest to use headers: form.getHeaders() in your request on line 27.

If that doesn’t do the trick, can you provide your code instead of a screenshot? That way we can copy/paste and attempt to reproduce.

My form.ejs code. My project stucture is as my image. I must take screenshot because I cannot paste my ejs code.

This is my index.js code. I use multer to upload file.

const express = require(“express”);
const app = express();
const multer = require(‘multer’)
const fetch = require(‘node-fetch’);
const FormData = require(‘form-data’);
const fs = require(‘fs’)
app.set(‘view engine’, ‘ejs’)
app.set(‘views’,’./views’)

// routes
app.get("/",(req,res)=>{
res.render(‘form’)
})

var storage = multer.diskStorage({
// destination: function(req,file,cb){
// cb(null,’./upload’)
// },
filename: function (req, file, cb) {
cb(null, file.originalname)
}
})

var upload = multer({ storage: storage })

app.post(’/upload’, upload.single(“file”), async function (req, res) {
console.log(req.file)
formdata = new FormData();
formdata.append(“upload”,fs.createReadStream(req.file.originalname))
await fetch(“http://localhost:8080/engine-rest/deployment/create”, {
method: ‘POST’,
headers: formdata.getHeaders(),
body: formdata,
redirect: ‘follow’
})
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log(‘error’, error));
res.send(“UPLOAD THANH CONG”)
})

// run app
app.listen(process.env.PORT || 3001, () =>
console.log(
Example app listening at http://localhost:${process.env.PORT || 3001}
)
);

I was able to get this working… the way you have this setup, you’re not streaming the file in memory, you have to grab it from disk from the full path, not just the filename. So this,

formdata.append(“upload”,fs.createReadStream(req.file.originalname))

should be

formdata.append('upload',fs.createReadStream(req.file.path))
1 Like

Thank you very much for your support. I deployed process successfully.