I’m currently building flask + react application where flask has a role of restful api. Now if a get request sent to the "/api/user/int:userid", it will be responded with json object containing some information about user.
So, my question is how do I route anyone’s request to let’s say "/profile/int:userid"? Should I be handling this on the frontend side, which sends a request to the api and if yes, then how do I actually implement it? I meen do I need somehow obtain a request params on the client-side and then send it to the backend with axios or whatever?
2
Answers
Yes, you should make request from the frontend. The idea is that the client makes a request and the server serves.
There are some choices available. You could make your own HTTP request. The API is there, so it’s possible. But you’ll probably want to use axios or some derivative of the fetch API. It’s been awhile since I’ve been doing frontend, so I’m not in a position to suggest the best solution. In the old days, I found the package I needed and ran it against npm trends to assert my decision or find alternatives.
first thing to do is to setup a page component in react and pass to it the user id like this :
<Route path="/profile/:userid" element={<UserProfile />} />
second thing is in
<UserProfile />
you need to grab that userId using useParams hook and send it with the request to flask :`import React, { useEffect, useState } from ‘react’;
import { useParams } from ‘react-router-dom’;
import axios from ‘axios’;
third, this is you should setup the endpoint to accept the userId (with auth):
that’s if I get what you want