I have searched all over to find out a simple way to ensure the data types in json passed to the fetch requests in react to backend in python(fastapi)
everything gets converted to string, but why is this? we all know not everything is a string, so why is this the default behavior???
here is what i have but i am getting 422 Unprocessable Entity
because requests keep getting all values as string
i have var3 as int
and var4 as float
here is part of the typescript
file
export const endpoint = async (var1: string, var2: string, var3: number, var4: number) => {
const jsonData = {'var1': var1, 'var2': var2, 'var3': var3, 'var4': var4};
const request = new Request('/api/create', {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
},
body: JSON.stringify(jsonData),
});
backend is fastapi/python backend
so here is what is getting passed with all values as string
{"var1'": "ehhtg", "var2": "knenjknke", "var3": "657784", "var4": "0.0035689"}
what do i need so i can have the backend to receive the json body as?
{"var1'": "ehhtg", "var2": "knenjknke", "var3": 657784, "var4": 0.0035689}
UPDATE:
this works as is, you can further ensure things with
const jsonData = {'var1': var1, 'var2': var2, 'var3': Number(var3), 'var4': Number(var4)};
but not necessary, the values with int and float were passed as expected
issue was a typo elsewhere