full-stack-web-development
full-stack-web-development copied to clipboard
post Endpoint (not working)
Bug description
I'm taking the Fullstack SvelteKit course and running into a bug at the 2:56:00
mark when trying to create the post Endpoint
https://youtu.be/OUzaUJ3gEug?t=10560
import type { RequestHandler } from "@sveltejs/kit";
// this breaks...
export const post: RequestHandler<{}, FormData> = (request) => {
console.log(request.body.get("text"));
}
When submitting the post request from the form, I get the 500 (Internal Server Error) and it appears that a new Breaking PR was merged in 7 days ago from rich that broke it https://github.com/sveltejs/kit/pull/3384
Can anyone recommend a good step-by-step for me to follow to fix?
Steps to reproduce
Follow the tutorial! :)
Expected behavior
No response
Example repository
You can see the finished code in question on Github https://github.com/gitpod-io/full-stack-web-development/blob/main/src/routes/todos/index.json.ts
Anything else?
No response
Description du bogue
Je suis le cours Fullstack SvelteKit et je rencontre un bogue à la
2:56:00
marque lorsque j'essaie de créer le **post Endpoint ** https://youtu.be/OUzaUJ3gEug?t=10560import type { RequestHandler } from "@sveltejs/kit"; // this breaks... export const post: RequestHandler<{}, FormData> = (request) => { console.log(request.body.get("text")); }
Lors de la soumission de la demande de publication à partir du formulaire, j'obtiens le 500 (erreur de serveur interne) et il semble qu'un nouveau Breaking PR a été fusionné il y a 7 jours par Rich qui l'a cassé sveltejs/kit#3384
Quelqu'un peut-il me recommander une bonne étape par étape à suivre pour réparer?
Étapes à reproduire
Suivez le tutoriel ! :)
Comportement attendu
Pas de réponse
Exemple de référentiel
Vous pouvez voir le code fini en question sur Github https://github.com/gitpod-io/full-stack-web-development/blob/main/src/routes/todos/index.json.ts
Rien d'autre?
Pas de réponse
hello, did you find a solution? I'm in the same situation as you.
here is my code
export const post = async ({ request }) => {
const t = await request.formData();
console.log(t.get('test'));
return {
status: 200,
headers: {
location: '/',
},
body: {
state: true,
},
};
};
my console.log() returns me a " file{} " an empty object. the "test" key exists but it is empty
if you solved your problem i would like some help thank you for your future response
hey @j314h , I actually did solve it!
This will get you halfway through the tutorial but I'm running into more issues as I get further along. But I'm confident in making it to the end. Still reading through the new SvelteKit docs and seeing how to update code that's changed.
Anyways, give this a try and let me know if it helps.
import type { RequestHandler } from "@sveltejs/kit";
// get
export const get: RequestHandler = async ({ request }) => {
return {
status: 200,
body: "Hello from the API!"
}
}
// post
export const post: RequestHandler = async ({ request }) => {
const formData = await request.formData();
return {
body: formData.get('text') as string
}
}
hey guys i had same Problem too. and i fixed it without RequestHandler. for that i read BodyParsing in SvelteKit Doc https://kit.svelte.dev/docs/routing#endpoints-body-parsing
Here's my code
export async function post({ request }: any) {
const data = await request.formData(); // or .json(), or .text(), etc
todos.push({
created_at: new Date(),
text: data.get('text') as string,
done: false
});
return {
status: 303,
headers: {
location: '/'
}
};
}
Huansock
Thank you Huansock, it works! Your answer is the best! It's just now you still need to change the word "post" into uppercase "POST", otherwise it still fails.