Return HTTP status code 201 in flask

前端 未结 9 1093
忘掉有多难
忘掉有多难 2020-12-23 19:58

We\'re using Flask for one of our API\'s and I was just wondering if anyone knew how to return a HTTP response 201?

For errors such as 404 we can call:



        
相关标签:
9条回答
  • 2020-12-23 20:46

    In my case I had to combine the above in order to make it work

    return Response(json.dumps({'Error': 'Error in payload'}), 
    status=422, 
    mimetype="application/json")
    
    0 讨论(0)
  • 2020-12-23 20:47

    In your flask code, you should ideally specify the MIME type as often as possible, as well:

    return html_page_str, 200, {'ContentType':'text/html'}
    
    return json.dumps({'success':True}), 200, {'ContentType':'application/json'}
    

    ...etc

    0 讨论(0)
  • 2020-12-23 20:52

    You can read about it here.

    return render_template('page.html'), 201
    
    0 讨论(0)
提交回复
热议问题