使用flask_restful构建RESTApi时出错

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

此处是RESTApi新手

我正在尝试使用.txt模块在我的代码中使用flask_restful在Flask中构建RESTapi服务(并且试图将输出另存为pydifact文件:

import datetime
from pydifact.message import Message
from pydifact.segments import Segment
from flask import Flask, request
from flask_restful import Resource, Api

app = Flask(__name__)

api = Api(app)


class RestAPI(Resource):
    def get(self, ABSENDER_ID, EMPFÄNGER_ID, ERSTELLUNG_DATUM_ZEIT, REFERENCE):
        MSCONS = Message()

        def erstellung_datum_zeit(dt_time):
            # Needed for the UNB segment
            dt_time = dt_time.strftime('%Y%m%d%H%M')
            return dt_time

        def UNA_UNB_segment(absender_id, empfänger_id, erst_datum, ref):

            MSCONS.add_segment(Segment('UNA', ":+.? '"))
            MSCONS.add_segment(Segment('UNB', ['UNOC', '3'], [absender_id, '14'], [
                               empfänger_id, '500'], [erst_datum[2:8], erst_datum[8:]], ref, '', 'TL'))

        ERSTELLUNG_DATUM_ZEIT = str(
            erstellung_datum_zeit(datetime.datetime.now()))
        UNA_UNB_segment(ABSENDER_ID, EMPFÄNGER_ID,
                        ERSTELLUNG_DATUM_ZEIT, REFERENCE)
        result = MSCONS.serialize()
        final_result = result

        PATH_FOR_TXT = r'C:\Users\kashy\OneDrive\Desktop\Codes\mscons.txt'
        textfile = open(PATH_FOR_TXT, 'w')
        textfile.write(result)
        textfile.close()

        return {'result': final_result}


api.add_resource(
    RestAPI,
    '/RestAPI/<int:ABSENDER_ID>/<int:EMPFÄNGER_ID/<int:ERSTELLUNG_DATUM_ZEIT/<int:REFERENCE>')

if __name__ == '__main__':
    app.run(debug=True)

ABSENDER_ID, EMPFÄNGER_ID, ERSTELLUNG_DATUM_ZEIT, REFERENCE都应该是用户输入,并且都应该是字符串格式。

[当我执行/RestAPI/<str:ABSENDER_ID>/<str:EMPFÄNGER_ID/<str:ERSTELLUNG_DATUM_ZEIT/<str:REFERENCE>时,出现以下错误:

C:\Users\kashy\OneDrive\Desktop\Codes\pydifact> & C:/Users/kashy/Anaconda3/envs/py36/python.exe c:/Users/kashy/OneDrive/Desktop/Codes/api.py
Traceback (most recent call last):
  File "c:/Users/kashy/OneDrive/Desktop/Codes/api.py", line 44, in <module>
    self.url_map.add(rule)
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 1401, in add
    rule.bind(self)
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 730, in bind
    self.compile()
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 790, in compile
    _build_regex(self.rule if self.is_leaf else self.rule.rstrip("/"))
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 779, in _build_regex
    convobj = self.get_converter(variable, converter, c_args, c_kwargs)
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 738, in get_converter
    raise LookupError("the converter %r does not exist" % converter_name)
LookupError: the converter 'str' does not exist

以及当我这样做时

/RestAPI/<int:ABSENDER_ID>/<int:EMPFÄNGER_ID/<int:ERSTELLUNG_DATUM_ZEIT/<int:REFERENCE>,出现以下错误:

PS C:\Users\kashy\OneDrive\Desktop\Codes\pydifact> & C:/Users/kashy/Anaconda3/envs/py36/python.exe c:/Users/kashy/OneDrive/Desktop/Codes/api.py
Traceback (most recent call last):
  File "c:/Users/kashy/OneDrive/Desktop/Codes/api.py", line 44, in <module>
    '/RestAPI/<int:ABSENDER_ID>/<int:EMPFÄNGER_ID/<int:ERSTELLUNG_DATUM_ZEIT/<int:REFERENCE>')
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\flask_restful\__init__.py", line 382, in add_resource
    self._register_view(self.app, resource, *urls, **kwargs)
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\flask_restful\__init__.py", line 448, in _register_view
    app.add_url_rule(rule, view_func=resource_func, **kwargs)
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\flask\app.py", line 98, in wrapper_func
    return f(self, *args, **kwargs)
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\flask\app.py", line 1277, in add_url_rule
    self.url_map.add(rule)
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 1401, in add
    rule.bind(self)
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 730, in bind
    self.compile()
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 790, in compile
    _build_regex(self.rule if self.is_leaf else self.rule.rstrip("/"))
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 766, in _build_regex
    for converter, arguments, variable in parse_rule(rule):
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 226, in parse_rule
    raise ValueError("malformed url rule: %r" % rule)
ValueError: malformed url rule: '/RestAPI/<int:ABSENDER_ID>/<int:EMPFÄNGER_ID/<int:ERSTELLUNG_DATUM_ZEIT/<int:REFERENCE>'

我对此并不陌生,只是开始使用Building a REST API using Python and Flask | Flask-RESTful教程学习它。

有人可以告诉我我在做什么错吗?

python python-3.x flask flask-restful
1个回答
1
投票

您的网址路由有问题。在第一个中,应该是string而不是str;在第二个中,您应该在>int:EMPFÄNGER_ID]的末尾缺​​少[C0

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