关于Todoism API出错问题

测试Web API出现以下错误,不知道原因:


(FlaskProjects-BArbVR56) D:\FlaskProjects>http :5000/api/v1/
HTTP/1.0 404 NOT FOUND
Content-Length: 82
Content-Type: application/json
Date: Thu, 28 Mar 2019 18:31:18 GMT
Server: Werkzeug/0.15.1 Python/3.7.1rc2

{
    "code": 404,
    "message": "The requested URL was not found on the server."
}


(FlaskProjects-BArbVR56) D:\FlaskProjects>http http://127.0.0.1:5000/api/v1
HTTP/1.0 404 NOT FOUND
Content-Length: 82
Content-Type: application/json
Date: Thu, 28 Mar 2019 18:36:25 GMT
Server: Werkzeug/0.15.1 Python/3.7.1rc2

{
    "code": 404,
    "message": "The requested URL was not found on the server."
}

部分源码:

class IndexAPI(MethodView):
    def get(self):
        return jsonify({
            "api_version": "1.0",
            "api_base_url": "http://127.0.0.1:5000/api/v1",
            "current_user_url": "http://127.0.0.1:5000/api/v1/user",
            "authentication_url": "http://127.0.0.1:5000/api/v1/token",
            "item_url": "http://127.0.0.1:5000/api/v1/items/{item_id}",
            "current_user_items_url": "http://127.0.0.1:5000/api/v1/user/items{?page,per_page}",
            "current_user_active_items_url": "http://127.0.0.1:5000/api/v1/user/items/active{?page,per_page}",
            "current_user_completed_items_url": "http://127.0.0.1:5000/api/v1/user/items/completed{?page,per_page}",
        })


api_v1.add_url_rule('/', view_func=IndexAPI.as_view('index'), methods=['GET'])  # 定义路由,把方法视图转化为视图函数

浏览器输入:http://127.0.0.1:5000/ 也显示出错了!


请指教!

路由注册代码部分呢?

你可以在命令行使用

flask routes

查看所有注册路由。


(FlaskProjects-BArbVR56) D:\FlaskProjects\FlaskHTTP\mytodoism>flask routes
Endpoint                Methods                  Rule
----------------------  -----------------------  --------------------------------
api_vi.active_items     GET                      /api/v1/user/items/active
api_vi.active_items     GET                      /v1/user/items/active
api_vi.completed_items  DELETE, GET              /api/v1/user/items/completed
api_vi.completed_items  DELETE, GET              /v1/user/items/completed
api_vi.index            GET                      /api/v1/
api_vi.index            GET                      /v1/
api_vi.item             DELETE, GET, PATCH, PUT  /api/v1/user/items/<int:item_id>
api_vi.item             DELETE, GET, PATCH, PUT  /v1/user/items/<int:item_id>
api_vi.items            GET, POST                /api/v1/user/items/<int:item_id>
api_vi.items            GET, POST                /v1/user/items/<int:item_id>
api_vi.token            POST                     /api/v1/oauth/token
api_vi.token            POST                     /v1/oauth/token
api_vi.user             GET                      /api/v1/user
api_vi.user             GET                      /v1/user
auth.login              GET, POST                /login
auth.logout             GET                      /logout
auth.register           GET                      /register
home.index              GET                      /
home.intro              GET                      /intro
home.set_locale         GET                      /set-locale/<locale>
static                  GET                      /static/<path:filename>
todo.app                GET                      /app
todo.clear_items        DELETE                   /item/clear
todo.delete_item        DELETE                   /item/<int:item_id>/delete
todo.edit_item          PUT                      /item/<int:item_id>/edit
todo.new_item           POST                     /items/new
todo.toggle_item        PATCH                    /item/<int:item_id>/toggle

(FlaskProjects-BArbVR56) D:\FlaskProjects\FlaskHTTP\mytodoism>http :5000/api/v1/
HTTP/1.0 404 NOT FOUND
Content-Length: 82
Content-Type: application/json
Date: Fri, 29 Mar 2019 01:43:32 GMT
Server: Werkzeug/0.15.1 Python/3.7.1rc2

{
    "code": 404,
    "message": "The requested URL was not found on the server."
}

出现这问题原因是:
settings.py:

class BaseConfig:
    ....
    SERVER_NAME = 'mytodoism.dev:5000'
    # enable subdomain support
    ...

加了这主机名就不行了。
hosts设置如下:

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost

# 127.0.0.1 activate.adobe.com 
# 0.0.0.0 account.jetbrains.com

127.0.0.1 mytodoism.dev
127.0.0.1 api.mytodoism.dev

我用的是window, 安装pipenv install win_inet_pton后,
在程序包__init__.py内:

import os
import click
import win_inet_pton
from flask import Flask, render_template, jsonify, request

不知是不是这样设置? 这样设置提示网址找不到

SERVER_NAME这个配置的确是个坑,我也掉进过,好像还有个ROOT什么什么的,千万不要随便设置。

那这个问题咋解决的呀?我也遇到了

window下设置hosts、设置SERVER_NAME = ‘mytodoism.dev:5000’,这样情况下,没解决!不知如何设置?子域方式搞不好!