How to debug http GET / POST Requests and its data payloads in Django REST Framework ?

This post is related to “How to create JSON objects with unique entries in Django REST Framework ?”

When we are sending some (json) data to servers from our machine / device using REST API’s, we normally send the JSON as payload data. But sometimes if JSON is not correctly formed, it becomes difficult to identify what is the problem and server sends generic “Bad Request” errors..

In such situations, it becomes necessary to print the contents of what we are receiving in payload in server so we can identify the problem.

So, in Django, we can print the data of http request as,

print(request.data)

and contents of the individual element in this data can be printed as,

userid_from_payload = request.data.get("userid", '')
print(userid_from_payload)

So, if we wants to debug POST, we need to write following debug statements in post as,

   def post(self, request, user_id, format=None):
        try :
            print(request.data)
            userid_from_payload = request.data.get("userid", '')
            print(userid_from_payload)
            userid_from_api = user_id
            print(userid_from_api)

In above code, “request.data” contains the complete payload of the request, hence it can be printed as,

print(request.data)

Now, if we want to know the individual element from this data / payload, then it can be accessed using “request.data.get()” as,

userid_from_payload = request.data.get("userid", '')

in above, we tried to get what client has sent in “userid” as part of JSON.

Related :   How to add Model to Django admin dashboard

so, lets say we tried to do http POST as below,

$ curl -v -k -X POST -H "Accept: application/json" -H "Content-Type:application/json" -d '{"userid":"my_userid","username":"lynxbee1","email":"social(at)lynxbee.com","age":"45", "useraddress":{"home_no":"123","street":"my home street","city":"my city","pincode":"123456"}}' http://192.168.0.106:8001/user_by_id/my_userid/

Then on server, we will see following debug logs printed as,

request.data => {'userid': 'my_incorrect_userid', 'username': 'lynxbee1', 'email': 'social(at)lynxbee.com', 'age': '45', 'useraddress': {'home_no': '123', 'street': 'my home street', 'city': 'my city', 'pincode': '123456'}}

userid_from_payload => my_userid

userid_from_api => my_userid


lynxbee_ezoic

Subscribe with Valid Email Id to receive updates in Inbox. ( Secured by Google FeedBurner )

   


Leave a Comment

Android Android Applications Android Build system Android Commands Application Libraries Application Stack / User Interface Bash / Shell Scripts Bluetooth driver Cloud Technologies Commands and Packages Compilation Content Management System Core Kernel C Programs Development & Build Development, Debugging and Performance Tools Development Environment Setup Django & REST Api Errors & Failures Git Hardware Platforms HTML JAVA Programs Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Multimedia Audio, Video, Images NDK / Middleware / HAL OS Concepts PHP Programming Languages Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Source Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Web design and development Website Hosting Wordpress Yocto / Bitbake / Openembedded