pythondocker

Flask REST API 404 Error When Trying to Access Endpoint


I am currently working on a Flask REST API that interacts with a Dockerized MySQL database. I have encountered a 404 error when trying to access the /api/create_account endpoint using cURL. Strangely, the index to '/' endpoint seems to be working fine

Endpoint: /api/create_account cURL Request:

curl --location 'http://localhost:5000/api/create_account' \
--header 'Content-Type: application/json' \
--data-raw '{
  "username": "new_username",
  "email": "new_email@example.com",
  "password": "new_password"
}'

error message:

<!doctype html>
<html lang=en>
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>

What I've Checked So Far:

Code Snippets: /api/create_account.

@app.route('/api/create_account', methods=['POST'])
def create_account():
    data = request.get_json()
    username = data.get('username')
    email = data.get('email')
    password = data.get('password')

    hashed_password = hash_password(password)
    write_to_Users('unique_google_id', username, email, 'UName', '2003-03-05', 'Male', 'Sample School')
    return jsonify(message='Account created successfully'), 200

Dockerfile

FROM python:3.8
EXPOSE 5000
WORKDIR /app
COPY requirements.txt /app
RUN pip install -r requirements.txt
COPY app.py /app
CMD python app.py

I would appreciate any insights into why I might be encountering a 404 error specifically for the /api/create_account endpoint. If you need more information or code snippets, please let me know. Thank you!


Solution

  • I think you're missing the -X POST option for the curl command, curl defaults to GET if no method is specified and you configured the /api/create_account route to receive POST requests only with

    @app.route('/api/create_account', methods=['POST'])