Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework debug post and put requests

I use DRF extension to se json list for model, and there i can debug with debug-toolbar that GET request, but how can i debug POST and PUT requests?

I have this for settings in debug mode:

INSTALLED_APPS += ('debug_toolbar',)

MIDDLEWARE_CLASSES += ('debug_toolbar.middleware.DebugToolbarMiddleware',)

DEBUG_TOOLBAR_PATCH_SETTINGS = False
INTERNAL_IPS = (
    '127.0.0.1'
)

Now, when i try with Intercept redirects in debug-toolbar, it doesn't show me toolbar when i do POST.

like image 550
Mirza Delic Avatar asked Sep 02 '16 08:09

Mirza Delic


People also ask

How to define routes in Django REST framework?

Define Django REST framework Routes When a client sends request to our Django Rest Api for an endpoint using HTTP request (GET, POST, PUT, DELETE), we need to determine how the server will response by defining the routes. Create a urls.py inside tutorials app with urlpatterns containing url s to be matched with request functions in the views.py:

How to implement Django REST API?

Step by step to implement Django Rest Api. 1 1. Technology. 2 2. Project structure. 3 3. Install Django REST framework. 4 4. Setup new Django project. 5 5. Setup new Django app for CRUD Rest Api. More items

How to send put and delete request in Django?

Django does not put data sent with a PUT or DELETE request in a request.PUT or request.DELETE dictionary, like it does with GET or POST data. This is for a good reason. What you can do to solve these problems? You can use the so called POST TUNNELLING method, that is using a POST request, putting the HTTP header X_METHODOVERRIDE in the request.

What is the use of request stream in Django?

request.stream returns a stream representing the content of the request body. You won't typically need to directly access the request's content, as you'll normally rely on REST framework's default request parsing behavior. As REST framework's Request extends Django's HttpRequest, all the other standard attributes and methods are also available.


2 Answers

I found django-silk for debugging DRF.

https://github.com/django-silk/silk/

like image 168
Mirza Delic Avatar answered Sep 20 '22 14:09

Mirza Delic


If you need to intercept the request/response and apply your own processing then you can add your custom mixin as described in this answer.

But in the most trivial scenario, given that you do a test POST request (or PUT), for example, with python requests:

import requests
response = requests.post('http://localhost:8000/person', json={"name": "dinsdale"})

Then you can get the error message with

print(response.text)

In most cases the output will contain the failure reason that you were looking for, e.g. 'age' is required.

You can also do the same thing with curl from a terminal:

curl -vv --header "Content-Type: application/json" \
    --request POST \
    --data '{"name":"dinsdale"}' http://localhost:8000/person/
like image 39
ccpizza Avatar answered Sep 23 '22 14:09

ccpizza