-
SummaryI have been trying to use fetch to submit
Packages are:
Additional informationFew issues I am having right now are:
Argument of type 'NextRequest' is not assignable to parameter of type 'IncomingMessage'.
Type 'NextRequest' is missing the following properties from type 'IncomingMessage': aborted, httpVersion, httpVersionMajor, httpVersionMinor, and 48 more.
Eventually I would like to pass some files that is why I am trying to stick to formData. Any suggestion on how to make next/formidable/typescript happy?
|
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 17 replies
-
OK. Scrap that. Next 13 can simply
|
Beta Was this translation helpful? Give feedback.
-
How can I save the file or read file data after this? let formData = await request.formData();
let body = Object.fromEntries(formData); I am sending only one pdf as the file and want to extract the inner text from the pdf. |
Beta Was this translation helpful? Give feedback.
-
I guess Also, I can't concert File object to Buffer. tsk tsk |
Beta Was this translation helpful? Give feedback.
-
For those who are using Supabase, I managed to do a test now that way. import { NextResponse, NextRequest } from "next/server";
import { cookies } from "next/headers";
import { createRouteHandlerClient } from "@supabase/auth-helpers-nextjs";
export async function POST(request: NextRequest) {
const supabase = createRouteHandlerClient({ cookies });
const data = await request.formData();
let body = Object.fromEntries(data);
const file = data.get("outdoor") as Blob | null;
const dataBody = data.get("data") as string;
const parsedBody = JSON.parse(dataBody);
try {
await supabase.storage
.from("bucket_test")
.upload(parsedBody.filename, file as File);
} catch (err) {
console.error({ error: err });
}
return NextResponse.json(body);
} It is important to define Policies on Storage. I found out that if not defined, the database does not save and shows a 403 in the log. On the front-end, I'm using React-Hook-Forms, I've tested it like this: const handleSubmitCreateUser = useMutation(
async (userData: UserFormSchema) => {
const formData = new FormData();
formData.append("file", userData.file as File);
let data = {
...userData,
filename: userData.file ? userData.file["name"] : "",
};
// @ts-expect-error Delete File to object data reference
delete data.file;
formData.append("data", JSON.stringify(data));
const result = await fetch("/api/test/create", {
method: "POST",
mode: "same-origin",
body: formData,
}).then((res) => res.json());
console.log(result);
}
); Sorry if the example wasn't very good. I also had trouble understanding it at first (nothing is documented). Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
i use code from https://stackoverflow.com/a/76379718/17577829 and it's worked |
Beta Was this translation helpful? Give feedback.
-
Just a note, this doesn't work with Works fine with |
Beta Was this translation helpful? Give feedback.
-
If anyone is looking for a working example (with app directory, fully typed and without dependencies) check this out: https://stackoverflow.com/a/77353442/8662476 |
Beta Was this translation helpful? Give feedback.
-
This solution is maybe not on the correct way, because it's not using formidable. You can see below
|
Beta Was this translation helpful? Give feedback.
OK. Scrap that. Next 13 can simply