node-formidable / formidable

The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.
MIT License
7.01k stars 681 forks source link

bad content-type header, unknown content-type: text/plain;charset=UTF-8 #495

Closed maskletter closed 5 years ago

maskletter commented 5 years ago

image image image

bad content-type header, unknown content-type: text/plain;charset=UTF-8

Can't receive such data?

maskletter commented 5 years ago
const form = new formidable.IncomingForm();

form.parse(req, function(err, fields, files) {
    if(err){
        console.log(err)
        next();
        return;
    }
     next(fields);
 });
//html
var xml = new XMLHttpRequest();
xml.open('post', '/bef/upload?f=123')
xml.onload = function(){
     console.log(xml.responseText)
}
var json = {
     name: 'tom'
}
xml.send(JSON.stringify(json))
xarguments commented 5 years ago

@maskletter, thanks for reporting.

As I understood your "content-type" is "text/plain;charset=UTF-8", right? In that case no need to use Formidable, Express/NodeJS already can parse it. (Formidable is mainly for handling file uploads with "content-type" equal to "multipart/form-data").

If I'm mistaken, please correct me. Waiting for your response, to decide whether we need to investigate or close the issue.

maskletter commented 5 years ago
return new Promise((resolve, reject) => {
            console.log('request')
            const form = new formidable.IncomingForm();

            form.onPart = function(part) {
                 if (!part.filename) {
                     form.handlePart(part);
                 }
            }

            form.parse(request, function(err, fields, files) {
                if(err){
                    throw err
                }
                console.log('aaaaaaaaaaaa')
                resolve(fields);
            });
        })

var xml = new XMLHttpRequest();
        xml.open('post', '/user/login2?f=123')
        xml.onload = function(){
            console.log(xml.responseText)
        }
        var json = {
            name: 'tom'
        }
        xml.send()

This code will output “aaaaaaaaaaaa”


var xml = new XMLHttpRequest();
        xml.open('post', '/user/login2?f=123')
        xml.onload = function(){
            console.log(xml.responseText)
        }
        var json = {
            name: 'tom'
        }
        xml.send(JSON.stringify(json))

This code will not have any output.


Is because formidable can't parse JSON.stringify(json), causing no return?

xarguments commented 5 years ago

@maskletter, Yes. JSON.stringify() will return a string. Then xml.send(String) will try to automatically set the Content-Type header, perhaps to undefined or to text/plain (unsure of it). But surely it will not set content type to multipart/form-data or something that is parseable for Formidable. Thus Formidable will ignore it, and what you send will get into req.body "as is".

ajmeese7 commented 1 year ago

@xarguments would it be possible to have a fallback for the text/plain;charset=UTF-8 data type implemented then? Since no intervention is needed on the part of formidable, you could just pass the data directly through to express as you suggested, thus preventing this from erroring out on plain text input.

This individual had the same error that I am having now, where even when uploading a non-text file like a photo the fields and files objects are empty and we get the error that this issue's OP experienced.