Next.js Discord

Discord Forum

Request body length does not match content-length header

Unanswered
Caillou posted this in #help-forum
Open in Discord
I need to redirect the user after sending the action, but doing so returns an error in the console.


failed to get redirect response TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:11413:11) {
  cause: RequestContentLengthMismatchError: Request body length does not match content-length header
      at write (C:\Users\caioc\Documents\GitHub\sobeit\node_modules\next\dist\compiled\undici\index.js:1:67105)    
      at _resume (C:\Users\caioc\Documents\GitHub\sobeit\node_modules\next\dist\compiled\undici\index.js:1:66726)  
      at resume (C:\Users\caioc\Documents\GitHub\sobeit\node_modules\next\dist\compiled\undici\index.js:1:65413)   
      at connect (C:\Users\caioc\Documents\GitHub\sobeit\node_modules\next\dist\compiled\undici\index.js:1:65301) {
    code: 'UND_ERR_REQ_CONTENT_LENGTH_MISMATCH'
  }
}



Code:
import prisma from '@/lib/prisma';
import { getUserSession } from '@/lib/utils';
import { redirect } from 'next/navigation';
import React from 'react';

export default async function Home() {
    async function action(formData: FormData) {
        'use server';

        const user = await getUserSession()

        await prisma.gamesSuggestions.create({
            data: {
                game: formData.get('name') as string,
                url: formData.get('url') as string,
                userId: user.id
            }
        })

        redirect('/notifications/sent')
    }

    await getUserSession();

    return (<>
        <div className='flex h-screen w-full justify-center items-center bg-[#ededed]'>
            <div className='border w-1/5 pb-8 shadow-lg rounded text-[#727272] font-semibold bg-white'>
                <div>
                    <form action={action} className='w-11/12 mx-auto pt-8 grid grid-cols-12 gap-x-6 gap-y-3'>
                        <div className='flex flex-col col-span-6'>
                            <label htmlFor='name' className='text-xs'>Name</label>
                            <input required name="name" type='text' className='border rounded-lg h-8 focus:outline-none pl-3 invalid:border-red-500' />
                        </div>
                        <div className='flex flex-col col-span-6'>
                            <label htmlFor='url' className='text-xs'>Game URL</label>
                            <input required name="url" type='url' className='border rounded-lg h-8 focus:outline-none pl-3 invalid:border-red-500' />
                        </div>
                        <div className='col-span-12 flex'>
                            <div className='flex justify-end float-right w-full'>
                                <button type='submit' className='bg-[#00c882] h-8 rounded-full mt-4 text-white w-32'>Save</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div >
        </div >
    </>)
}

3 Replies

Old English Sheepdog
did you ever figure this out. running into the same issue with server actions and redirect
American Crocodile
Hitting the same issue - there's an open issue here: https://github.com/vercel/next.js/issues/48744
@American Crocodile Hitting the same issue - there's an open issue here: https://github.com/vercel/next.js/issues/48744
Old English Sheepdog
Just try catch for now and ignore the exception