I'm having hard times with this package of busboy in some app I'm trying to build wanting to upload images from/to my firebase... Already tried to look at some of your possible solutions with the same problems but perhaps different code and it didn't work. May I share mine, so you might give me an idea about what is going wrong? Here the lines:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp()
const path = require('path');
const os = require('os');
const cors = require('cors')({ origin: true });
const BusBoy = require('busboy')
const fs = require('fs');
const gcConfig = {
projectId: 'meeting-scheduler-9cee1',
keyFilename: "meeting-scheduler-9cee1-firebase-adminsdk-3unr4-09df5fe69a.json"
}
const { Storage } = require("@google-cloud/storage");
exports.upLoadFile = functions.https.onRequest((request, response) => {
cors(request, response,() => {
const busBoy = new BusBoy({ headers: request.headers});
let uploadData = null;
if (request.method !== "POST") {
return response.status(500).json({
message: 'Not Allowed!!'
})
}
// if (!request.headers['Content-Type']){
// return next(new Error('Bad request'));
// }
// else
// next();
busBoy.on('file', (fieldname, file, filename, encoding, mimetype) => {
const filepath = path.join(os.tmpdir(), filename);
uploadData = { file: filepath, type: mimetype };
file.pipe(fs.createWriteStream(filepath));
});
busBoy.on('finish', () => {
const bucket = gcs.bucket('meeting-scheduler-9cee1.appspot');
bucket
.upload(uploadData.file, {
uploadType: 'media',
metadata: {
metadata: {
contentType: uploadData.type
}
}
})
.then(() => {
response.status(200).json({
message: 'It Works!'
});
})
.catch(err => {
response.status(500).json({
error: err
})
})
});
busBoy.end(request.rawBody);
});
});
Any time I fire the function in my firebase de error is the same,I'm sharing it too:
Error: Missing Content-Type
at new Busboy (/srv/node_modules/busboy/lib/main.js:23:11)
at cors (/srv/index.js:58:24)
at cors (/srv/node_modules/cors/lib/index.js:188:7)
at /srv/node_modules/cors/lib/index.js:224:17
at originCallback (/srv/node_modules/cors/lib/index.js:214:15)
at /srv/node_modules/cors/lib/index.js:219:13
at optionsCallback (/srv/node_modules/cors/lib/index.js:199:9)
at corsMiddleware (/srv/node_modules/cors/lib/index.js:204:7)
at exports.upLoadFile.functions.https.onRequest (/srv/index.js:57:5)
at cloudFunction (/srv/node_modules/firebase-functions/lib/providers/https.js:49:9)
I'm having hard times with this package of busboy in some app I'm trying to build wanting to upload images from/to my firebase... Already tried to look at some of your possible solutions with the same problems but perhaps different code and it didn't work. May I share mine, so you might give me an idea about what is going wrong? Here the lines:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp()
const path = require('path');
const os = require('os');
const cors = require('cors')({ origin: true });
const BusBoy = require('busboy')
const fs = require('fs');
const gcConfig = {
projectId: 'meeting-scheduler-9cee1',
keyFilename: "meeting-scheduler-9cee1-firebase-adminsdk-3unr4-09df5fe69a.json"
}
const { Storage } = require("@google-cloud/storage");
exports.upLoadFile = functions.https.onRequest((request, response) => {
cors(request, response,() => {
const busBoy = new BusBoy({ headers: request.headers});
let uploadData = null;
if (request.method !== "POST") {
return response.status(500).json({
message: 'Not Allowed!!'
})
}
// if (!request.headers['Content-Type']){
// return next(new Error('Bad request'));
// }
// else
// next();
busBoy.on('file', (fieldname, file, filename, encoding, mimetype) => {
const filepath = path.join(os.tmpdir(), filename);
uploadData = { file: filepath, type: mimetype };
file.pipe(fs.createWriteStream(filepath));
});
busBoy.on('finish', () => {
const bucket = gcs.bucket('meeting-scheduler-9cee1.appspot.');
bucket
.upload(uploadData.file, {
uploadType: 'media',
metadata: {
metadata: {
contentType: uploadData.type
}
}
})
.then(() => {
response.status(200).json({
message: 'It Works!'
});
})
.catch(err => {
response.status(500).json({
error: err
})
})
});
busBoy.end(request.rawBody);
});
});
Any time I fire the function in my firebase de error is the same,I'm sharing it too:
Error: Missing Content-Type
at new Busboy (/srv/node_modules/busboy/lib/main.js:23:11)
at cors (/srv/index.js:58:24)
at cors (/srv/node_modules/cors/lib/index.js:188:7)
at /srv/node_modules/cors/lib/index.js:224:17
at originCallback (/srv/node_modules/cors/lib/index.js:214:15)
at /srv/node_modules/cors/lib/index.js:219:13
at optionsCallback (/srv/node_modules/cors/lib/index.js:199:9)
at corsMiddleware (/srv/node_modules/cors/lib/index.js:204:7)
at exports.upLoadFile.functions.https.onRequest (/srv/index.js:57:5)
at cloudFunction (/srv/node_modules/firebase-functions/lib/providers/https.js:49:9)
Share
Improve this question
edited Dec 29, 2019 at 17:58
Enrique GF
asked Dec 29, 2019 at 16:04
Enrique GFEnrique GF
1,2954 gold badges19 silver badges38 bronze badges
7
-
Side note: You should use
if (request.method !== "POST") { return response.status(405).set('Allow', 'POST').json({ message: 'Not Allowed!!' }) }
– samthecodingman Commented Dec 29, 2019 at 17:22 - Can you share your client code where you are making the request? – samthecodingman Commented Dec 29, 2019 at 17:22
-
Also, don't forget to handle errors from
cors
usingcors(request, response, (err) => { if (err) { /* do something */ return; } /* ... */ })
– samthecodingman Commented Dec 29, 2019 at 17:26 -
Lastly, rather than use
busBoy.end(request.rawBody);
, userequest.pipe(busBoy);
for better performance – samthecodingman Commented Dec 29, 2019 at 17:29 - ok i already add the request made through postman, and tried to apply your advices on my code, but still doesnt work – Enrique GF Commented Dec 29, 2019 at 17:59
3 Answers
Reset to default 13Ran into this myself. Turns out, main.js in the busboy module checks for content-type
instead of Content-Type
, and thus does not find the header. Confirmed this was the issue by fixing the casing in main.js (which isn't a feasible solution in most cases), after which the constructor call worked on my request, as expected.
Apparently this was brought up before, but the issue was closed (link).
For anyone using postman
and encounters this issue: I didn't realize the file postman was referencing no longer existed, thus content-type
didn't exist, but postman
never plained. I switched to a valid file and it worked.
As Kacey pointed out, busboy throws this error because the HTTP request does not contain a Content-Type
header.
HTTP headers are by default written in lowercase once it reaches Node, no matter how you might have defined them in the frontend for instance, hence causing this error.
I solved it by simply adding the following lines to my code:
app.use((req, res, next) => {
req.headers['Content-Type'] = req.headers['content-type']; next();
})
app.use(busboy());