Works on Localhost, but not in Prod
Answered
Asiatic Lion posted this in #help-forum
Asiatic LionOP
I have a api/checkout endpoint where i call fetch /email/send and it throws an error in production but works in localhost
Checkout endpoint
Sending email endpoint
Checkout endpoint
...
console.log("[Checkout]: Sending email");
fetch(`${process.env.NEXTAUTH_URL}/api/email/printable`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
id: printableRequest.id,
email: printableRecipient.email,
firstName: printableRecipient.firstName,
lastName: printableRecipient.lastName,
printables: printableProductsData.map((product) => product.name),
pdfUrl: secure_url,
}),
});
console.log("[Checkout]: Webhook sent");
...Sending email endpoint
export async function POST(req: NextRequest) {
const body: BodyType = await req.json();
const { id, email, firstName, lastName, printables, pdfUrl } = body;
console.log("[Printable Email Request]: Sending email to", email);
try {
const emailId = await sendEmail({
to: email,
subject: `Your printable request from ${config.appName}`,
react: PrintablesEmailTemplate({
name: `${firstName} ${lastName}`,
printables,
pdfUrl: pdfUrl,
}),
});
console.log("[Printable Email Request]: Email sent to", email);
await PrintableRequest.findByIdAndUpdate(id, {
emailId: emailId,
status: emailId ? "pending" : "error",
});
console.log(
`[Printable Email Request]: Updated printable request ${id} - ${
emailId ? "pending" : "error"
}`
);
return NextResponse.json(
{
success: true,
emailId: emailId,
},
{
status: STATUS_CODES.success.OK,
}
);
} catch (error) {
console.error(error);
return NextResponse.json("Error sending email. Please try again later.", {
status: STATUS_CODES.serverError.INTERNAL_SERVER_ERROR,
});
}
}Answered by Asiatic Lion
Thank you so much @Common Moorhen, your answer leads me to this stackoverflow answer https://stackoverflow.com/a/77761887/13647789. the domain i used has www.
6 Replies
Asiatic LionOP
Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"TypeError: fetch failed","reason":{"errorType":"TypeError","errorMessage":"fetch failed","cause":{"errorType":"RequestContentLengthMismatchError","errorMessage":"Request body length does not match content-length header","code":"UND_ERR_REQ_CONTENT_LENGTH_MISMATCH","name":"RequestContentLengthMismatchError","message":"Request body length does not match content-length header","stack":["RequestContentLengthMismatchError: Request body length does not match content-length header"," at AsyncWriter.end (node:internal/deps/undici/undici:9128:19)"," at writeIterable (node:internal/deps/undici/undici:9032:16)"]},"stack":["TypeError: fetch failed"," at Object.fetch (node:internal/deps/undici/undici:11731:11)"," at async globalThis.fetch (/var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:36091)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: TypeError: fetch failed"," at process.<anonymous> (file:///var/runtime/index.mjs:1276:17)"," at process.emit (node:events:529:35)"," at emit (node:internal/process/promises:149:20)"," at processPromiseRejections (node:internal/process/promises:283:27)"," at process.processTicksAndRejections (node:internal/process/task_queues:96:32)"]}Common Moorhen
it looks like the requests is containing a
idk how to fix this, because i think next js uses
see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Length
Content-Lenght header, and is missmatching from the actual body size.idk how to fix this, because i think next js uses
Content-Lenght internally to validate the actions and endpoints max body size, so it may be a bug? idk.see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Length
Asiatic LionOP
Thank you so much @Common Moorhen, your answer leads me to this stackoverflow answer https://stackoverflow.com/a/77761887/13647789. the domain i used has www.
Answer
Common Moorhen
glad you could fix it!
when you confirm it works, mark the answer to close the thread