Return HTTP status code 201 in flask

前端 未结 9 1132
忘掉有多难
忘掉有多难 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:37

    Dependent on how the API is created, normally with a 201 (created) you would return the resource which was created. For example if it was creating a user account you would do something like:

    return {"data": {"username": "test","id":"fdsf345"}}, 201
    

    Note the postfixed number is the status code returned.

    Alternatively, you may want to send a message to the client such as:

    return {"msg": "Created Successfully"}, 201
    

提交回复
热议问题