Not been able to add POST api method using Flask

Hi,
Below is the POST method I added:
@app.route(‘/greet’, methods=[‘POST’])
def greet():
“”"
Handles POST requests to the /greet endpoint.
Expects JSON data with a ‘name’ field.
“”"
data = request.get_json()
if data and ‘name’ in data:
name = data[‘name’]
return jsonify({“message”: f"Hello, {name}!"}), 200
else:
return jsonify({“error”: “Invalid request. Please provide ‘name’ in JSON data.”}), 400

Getting the error code 404 and text response as ‘Cannot POST /spaces/name/space/v1/greet’

1 Like

Hmm… Like this issue?

Throw this in there to help you debug a bit better.
if data and ‘name’ in data:
name = data[‘name’]
return jsonify({“message”: f"Hello, {name}!"}), 200
else:
return jsonify({“error”: “Invalid request. Please provide ‘name’ in JSON data.”}), 400

The frontend or client is POSTing to a different path or the data is in a bad format and you aren’t grabbing name.

Try a curl on the api to test it as well.

1 Like