尝试访问端点时出现 Flask REST API 404 错误

问题描述 投票:0回答:1

我目前正在开发一个与 Docker 化的 MySQL 数据库交互的 Flask REST API。我在尝试使用 cURL 访问 /api/create_account 端点时遇到 404 错误。奇怪的是,“/”端点的索引似乎工作正常

端点:/api/create_account 卷曲请求:

curl --location 'http://localhost:5000/api/create_account' \ --header 'Content-Type: application/json' \ --data-raw '{ "username": "new_username", "email": "[email protected]", "password": "new_password" }'

错误信息:

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

到目前为止我检查过的内容:

  • 验证是否为 /api/create_account 正确定义了 Flask 路由。
  • 更改代码后重新启动 Flask 服务器。
  • 检查 Flask 应用程序中的其他端点是否可访问。
  • 确保 Docker 容器位于同一网络上。

代码片段: /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

如果您能深入了解为什么我可能会遇到专门针对 /api/create_account 端点的 404 错误,我将不胜感激。如果您需要更多信息或代码片段,请告诉我。谢谢!

python docker flask-sqlalchemy
1个回答
0
投票

我认为您缺少

-X POST
命令的
curl
选项,如果未指定方法,
curl
默认为
GET

© www.soinside.com 2019 - 2024. All rights reserved.