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

FormData uses chunked encoding rather than Content-Length, even for known-sized form uploads #21626

Open
litttley opened this issue Dec 18, 2023 · 2 comments
Labels
ext/fetch related to the ext/fetch ext/web related to the ext/web crate web related to Web APIs

Comments

@litttley
Copy link

litttley commented Dec 18, 2023

Version: Deno 1.39.0

for formData request build can't got content-length header

 

const hander=async (req)=>{
   let data
    if (req.method === 'POST') {
            const formData = await req.formData()

            const file = formData.get('file')
 
            const FromEmail = formData.get('FromEmail')
            const ToEmail1 = formData.get('ToEmail1')
            const MessageText = formData.get('MessageText')
        
            let formDataNew = new FormData();
    
 
            formDataNew.append("FromEmail", FromEmail);
       
            formDataNew.append("ToEmail1",ToEmail1);
 
            formDataNew.append("MessageText",MessageText);
            formDataNew.append("upfile", file, file.name);

              const request = new Request("https://post.deno.dev", {
                 method: "POST",
                body: formDataNew });

                console.log(request.headers); 

                //todo 

               // fetch(request)
 

 
            data = { fileName:file.name  ,MessageText,FromEmail,ToEmail1,headers:{
                'content-type':request.headers.get('content-type'),
                'content-length':request.headers.get('content-length'),
            }}

            console.info(data)
        }

         const res = new Response(
            `<!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="utf-8">
                <title>repro</title>
            </head>
            <body>
                <form method="post" action="" enctype="multipart/form-data">
                    <p><input type="file" required name="file"></p>
                    <p><input type="text" name="FromEmail" placeholder="FromEmail"  ></p>
                    <p><input type="text" name="ToEmail1" placeholder="ToEmail1"  ></p>
                    <p><input type="text" name="MessageText" placeholder="MessageText"  ></p>
                    
                    <p><input type="submit"></p>
                </form>
                ${data
                    ? `<pre>${
                        JSON.stringify(data, null, 4).replaceAll(/[&<>'"]/g, (m) => `&#${m.codePointAt(0)!};`)
                    }</pre>`
                    : ''
                }
            </body>
            <script>
                const file = new File(['file'], '选择文件')
                const dt = new DataTransfer()
                dt.items.add(file)

                document.querySelector('[name=file]').files = dt.files
            </script>
            </html>`,
            { headers: { 'Content-Type': 'text/html' } }
        )

 return res
}


Deno.serve(hander);

@mmastrac mmastrac added the ext/fetch related to the ext/fetch label Dec 18, 2023
@mmastrac mmastrac changed the title can't got content-length header for formdata Request build FormData uses chunked encoding rather than Content-Length, even for known-sized form uploads Dec 18, 2023
@mmastrac mmastrac added ext/web related to the ext/web crate web related to Web APIs labels Dec 18, 2023
@mmastrac
Copy link
Contributor

We currently don't provide a Content-Length for FormData because the object is streamed as a Blob.

@crowlKats Is this something we can do without making the FormData serialization too slow?

@Kequc
Copy link

Kequc commented Nov 2, 2024

From what I understand part of the reason is that Content-Length is untrusted. Requests can include a content length that is inaccurate.

For my purposes, I'd like to set a maximum upload size for example. Where if that size is exceeded the request is terminated. Unfortunately I need to check the length as I go because I cannot trust content length. This is a problem because then I have my request body in a Blob and request.formData() isn't available anymore.

Some way I could parse the request data from a blob would be super. Or, a way to ask deno to limit the request size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ext/fetch related to the ext/fetch ext/web related to the ext/web crate web related to Web APIs
Projects
None yet
Development

No branches or pull requests

3 participants