Simple Django middleware for parsing JSON data from HTTP request.
If the Content-Type of a POST request is application/json, the JSON data is parsed and stored in the HttpRequest.json attribute.
- Default (Not accessed):
None - Success:
dict - Error: Empty
dict
from django.http import JsonResponse
def json_check(request):
if request.method == "POST":
data = request.json
return JsonResponse({'message': 'JSON received', 'data': data.get('message')})
else:
return JsonResponse({'error': 'Invalid content type or method'}, status=400)For more details on Django middleware, refer to the Django Middleware Documentation.
This project is licensed under the MIT License.
Improve the code and adjust it according to your project's needs. Contributions and suggestions are welcome!