![]() Here are few examples on how to use api_view 'POST']) But the decorator removes that as decorator by default only works for GET requests for other request types we need to mention it specifically. If you would have noticed traditional django POST request expects CSRF token. This automatically add “request” object to functions and expects a “response” object. Return decorator is a generic function based decorator used in DRF for rest api’s. This allows us to convert query params to a dict and we can easily use this in our code.Īlso to read JSON data in post, put requests we use it like this dummy(request): This allows us to read request data and query parameters easily. This is one of the things “response” does among other things. Notice how the response type changed to “JSON” automatically. Now if you run the route in postman notice the response We will see what is “api_view” decorator later on. To use “Response” you need to use “api_view” without that you will get error. Now lets use “Response” from rest_framework.response import Responseįrom rest_corators import index(request): If you run this on postman you will see a response We previously created a route “index” like this def index(request): To see this in action let’s use this in our code The main purpose of this is to be able to return json data types, set headers, response code easily which is required in rest apis’ You can have a read over them as well from the above links to understand the basics ResponseĭRF has introduced a new object “Response” instead of Django’s HttpResponse In this part we will see about “Views” with DRF
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |