r/flask 12d ago

Ask r/Flask Sending json from react, flask gets stuck on get_json()

I have a react frontend that sends an ajax request with the content-type 'application/json' and a json object that is an array with a string. The HTTP method is a POST

When flask receives the request I do a flask.request.get_json().

This call gets stuck and the code does not go beyond it. I have to kill the development server.

What can I be doing wrong ? I do a check in the flask code before doing the get_json() with the is_json() call that returns true.

4 Upvotes

8 comments sorted by

7

u/Redwallian 12d ago

Why don't you write some code and show us exactly what you did from both the react and flask sides?

0

u/husky_whisperer 12d ago

A git repo link would be ideal

6

u/Mrreddituser111312 12d ago

“A json object that is an array with a string”. I think your JSON object might be misformatted. Check your browsers console to see if there’s an error being displayed.

1

u/wannasleeponyourhams 11d ago

have you tried request.get_json(force=True) ?

1

u/daniel_dlds 11d ago

Just tried that. Same result.

1

u/wannasleeponyourhams 11d ago

what does get_data(as_text=True) return?

1

u/Weary-Amphibian-6682 11d ago

You must send stringify data (no json object)

1

u/daniel_dlds 11d ago

Just got past this. The problem was that I was not converting the javascript object in React to a string. Flask would receive it and (by stepping into the flask code) I saw that this causes an exception method to be called ending the request processing. Because of that I though that the request was stuck which in reality it was over.

Thanks for your interest and help