I've built a REST API in Python that allows users to register with an endpoint at @app.post("/register/{id}/{username}/{surname}"). On the front end, I have a registration page created with HTML and CSS, but I'm completely stuck on how to connect this page to my API. Do I need to use JavaScript for this integration? I'd love to learn the right way to do it, so any guidance would be greatly appreciated!
1 Answer
It looks like your API is set up to accept a POST request at that endpoint. To get the data from your HTML page to the API, you indeed need to use JavaScript! A simple way to do this is to create an HTML form that POSTs the data to your API. Just remember that when using a regular HTML form, it usually refreshes the page which can lose your state. To avoid that, start by creating an HTML form with the `method` set to 'post' and `action` pointing to your API endpoint. You can experiment by submitting the form and seeing the server response! Once you're comfortable, try using `fetch` in JavaScript to send your form data without reloading the page. It's a great way to learn the basics!
Thanks for the tip, but I’m still feeling a bit lost. Can you break it down even more? Like, what does my HTML form need to actually look like?