I am having a problem with getting my flask backend to send a response back to my REACT js frontend. The following is the code used in REACT js frontend create a post request sending the string, 'message'. The flask backend should then send the string back and a console log will be created displaying the string.
REACT JS
const response2 = await fetch(`${process.env.REACT_APP_TEST}`,{
method: 'POST',
credentials: 'include',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify('message'),
}
)
console.log('The response was....', response2)
Flask
@app.route("/", methods=['GET', 'POST'])
@cross_origin(supports_credentials=True)
def index():
print('Running')
print
sent_data = request.get_json()
print(sent_data)
if sent_data != 'message':
return ({"hello": ['no']})
else:
return (sent_data)
The data is picked up on the backend and prints "message" so information is getting to flask. The issue is that instead of receiving the string 'message' logged in the React js console I get...
The response was.. {type: "cors", url: "http://127.0.0.1:5000/", redirected: false, status: 200, ok: true, statusText: "OK", headers: Headers, body: ReadableStream, bodyUsed: false}
If anything is unclear let me know and appreciate the help, thanks!
fetchin wring way. It doesn't returnresponsefrom server but only information where it send data - objectPromise. You should use ratherfetch().then(...)orrequests = await fetch()andrequests.then(...)like in doc Using_Fetchreturn jsonify(send_data)and in reactresponse2 = response2.then(resp => resp.json())