AttributeError: 'Request' object has no attribute 'get'

岁酱吖の 提交于 2020-03-23 06:44:43

问题


When i make a POST request to my server, i get a 500 ERROR with comment:

AttributeError: 'Request' object has no attribute 'get'

This is my server:

@app.route('/api/entries', methods = ['POST'])
def create_entry():
    if not request.json:
        abort(400)
    entry = {
        'id': entries[-1]['id'] + 1,
        'Title': request.get('Title', ""),
        'Description': request.get('Description', ""),
        'Info': request.get('Info', "")
    }
    entries.append(entry)
    return jsonify( { 'entry': entry } ), 201

here is also my entries array:

entries = [
    {
        'id': 1,
        'Title': 'baradum',
        'Description': 'desc 1', 
        'Info': 'info1',
    },
    {
        'id': 2,
        'Title': 'jasd',
        'Description': 'desc 2', 
        'Info': 'info 2',
    }
]

What causes the problem?


回答1:


Just as i posted this question, i found the answer:

I needed to change

'Title': request.get('Title', ""),

to

'Title': request.json.get('Title', ""), 


来源:https://stackoverflow.com/questions/23661276/attributeerror-request-object-has-no-attribute-get

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!