Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
maskletter opened this issue Dec 6, 2018 · 5 comments
Closed

Comments

@maskletter
Copy link

image
image
image

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

Can't receive such data?

@maskletter
Copy link
Author

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
Copy link
Collaborator

@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
Copy link
Author

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
Copy link
Collaborator

@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
Copy link

ajmeese7 commented Aug 2, 2022

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants