Next.js Discord

Discord Forum

Server action fails when special characters are included (ü, ß)

Answered
Arboreal ant posted this in #help-forum
Open in Discord
Arboreal antOP
Hey all,

I've been having a really weird issue where only one of my users can't submit a post action. It turns out that when you include special characters (in this case some German characters, but I've also tested with some other diacritics such as ñ) the post request fails.

Nextjs throws this error:

 ⨯ TypeError: fetch failed
    at async enquiriesPost (./src/common/gen/api/EnquiriesApi.ts:52:17)
    at async createEnquiry (./src/components/Enquiries/actions/enquiry.actions.tsx:19:5)
Cause: RequestContentLengthMismatchError: Request body length does not match content-length header
    at AsyncWriter.write (node:internal/deps/undici/undici:9068:19)
    at writeIterable (node:internal/deps/undici/undici:9028:23) {
  code: 'UND_ERR_REQ_CONTENT_LENGTH_MISMATCH'
}


It's very opaque as to what's causing this issue.

Here's the code for the post request:
export const enquiriesPostFetch = ({
  body,
  ...requestInitArgs
}:{
  body: EnquiriesPost,
} & Omit<RequestInit, 'body'>): Promise<HttpResponseType<Enquiry>> => {
  const { headers: reqHeaders, ...requestInitArgsRest } = requestInitArgs ?? {};

  let headers: Headers;

  if(reqHeaders) {
    headers = new Headers(reqHeaders);
  } else {
    headers = new Headers();
  }

  headers.set('Content-Type', 'application/json');
  const bodyStr = JSON.stringify(body);
  headers.set('Content-Length', `${bodyStr.length}`);

  console.log({bodyStr, len: bodyStr.length})

  return fetch(`${baseUrl}/enquiries`, {
    body: JSON.stringify(body),
    headers,
    method: 'POST',
    next: {
      tags: [
        enquiriesTag()
      ],
    },
    ...requestInitArgsRest
  });

};


The console log for some test data outputs:

{
  bodyStr: '{"title":"ññ","body":"ññ","matterId":"5a692cb6-fedc-4269-a436-fd3a993b39d8","documentId":"f7594d10-a926-4a40-841f-3a011dbf4c4a"}',
  len: 128
}
Answered by linesofcode
content length isnt the length of a string
View full answer

8 Replies

Arboreal antOP
I had to add the content length stuff previously because of some undicii errors, but it seems that that's where it's going wrong. The string.length is producing a different length than the actual content length because the special characters are a different byte length. Is there a better way of calculating the content length? If not I'll revert the manual setting of the content length as I think I may have resolved that issue.
content length isnt the length of a string
Answer
its the size of the body in bytes
your logic is off
I would try to omit content-length altogether
Arboreal antOP
Nice thanks for confirming. I've removed the content length header and got it working. I had it in because previously I was passing in the entire next headers to the fetch so the cookie would get passed along, but that was breaking when the cache was invalidated as the content length was set to 0. I only pass in the cookies header now so it works great without it.
Lilac
Thanks for this discussion. My deployment broke today and it was because headers.content-length = 0 was now in my headers passed along to a fetch - no idea why that started.
Arboreal antOP
Nice! Glad it helped 🙂