13

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.

Mirza Delic
  • 3,591
  • 12
  • 46
  • 79
  • check this http://stackoverflow.com/questions/1118183/how-to-debug-in-django-the-good-wayand personally when I work with api's the best way to debug/code is by writing tests – pleasedontbelong Sep 02 '16 at 09:46

4 Answers4

20

I found django-silk for debugging DRF.

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

Mirza Delic
  • 3,591
  • 12
  • 46
  • 79
4

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/
ccpizza
  • 21,405
  • 10
  • 121
  • 123
0

You can not intercept redirects in DRF browsable api because it is ajax and called via javascript. Also the toolbar is not shown because of ajax call. As workaround you could temporary comment lines in debug_toolbar.middleware.DebugToolbarMiddleware#process_request that disables toolbar in ajax call:

    ...
    # Don't render the toolbar during AJAX requests.
    # if request.is_ajax():
    #     return
    ...

Redirects still would not work, but toolbar would be visible.

FeroxTL
  • 3,671
  • 2
  • 14
  • 12
0

When debugging interactively on a local machine, I like to examine the request data by breaking in the relevant put() or post() method in rest_framework.generics.

For quick local debugging of POST or PUT validation errors (e.g. in case of http status 400 issues), I like to break at the end of Field.run_validators() in rest_framework.fields and examine the error messages there.

djvg
  • 5,523
  • 3
  • 36
  • 64