Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<!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:

  • Verified that the Flask route is correctly defined for /api/create_account.
  • Restarted the Flask server after making changes to the code.
  • Checked that other endpoints in the Flask app are accessible.
  • Ensured that the Docker containers are on the same network.

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'])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading