I've been working on an app with a React.js frontend and a Python Flask backend (REST API), and one thing I want is for a form to send JSONified data from React to my Flask backend via a POST request, so that it can be processed.
However, I keep getting a 404 (not found) HTTP error, and it's really frustrating because I genuinely don't know how to debug this. I'm not sure what happens in between the request and why this is happening.
Here's the relevant React frontend code:
handleSubmit = (e) => {
e.preventDefault();
const text = {
text : this.state.content
};
const sendText = {
method: 'POST',
headers: { 'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(text)
};
console.log(sendText.body);
const url = 'https://cors-anywhere.herokuapp.com/http://localhost:5000/memes';
fetch(url, sendText).then(response => response.json())
.then(data => console.log(data));
console.log("Success!")
And here's the relevant Flask code
@app.route('/memes', methods=['POST'])
def memes():
data = request.get_json()
text = data["text"]
return jsonify({"result": "success!", "text": text}), 200
Any idea what's wrong? Please feel free to ask any questions about what I'm doing or what I'm using.
Thanks in advance!