From 0d9c016a166f5ef5f72cf29663693ca32c6c91f8 Mon Sep 17 00:00:00 2001 From: Guillaumebeuzeboc Date: Mon, 25 Mar 2024 15:18:53 +0100 Subject: [PATCH] fix(API): browser post request generated error 500. Parsing the request consumes it. And posting from the browser calls post then get. --- cos_registration_server/api/views.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/cos_registration_server/api/views.py b/cos_registration_server/api/views.py index 092ce32..2f395ae 100644 --- a/cos_registration_server/api/views.py +++ b/cos_registration_server/api/views.py @@ -11,7 +11,6 @@ from devices.models import Device from django.http import HttpResponse from rest_framework.exceptions import NotFound -from rest_framework.parsers import JSONParser from rest_framework.request import Request from rest_framework.response import Response from rest_framework.views import APIView @@ -38,8 +37,7 @@ def post(self, request: Request) -> Response: request: Http GET request. return: Http JSON response. """ - data = JSONParser().parse(request) - serialized = DeviceSerializer(data=data) + serialized = DeviceSerializer(data=request.data) if serialized.is_valid(): serialized.save() return Response(serialized.data, status=201) @@ -75,8 +73,7 @@ def patch(self, request: Request, uid: str) -> Response: return: Http JSON response. """ device = self._get_device(uid) - data = JSONParser().parse(request) - serialized = DeviceSerializer(device, data=data, partial=True) + serialized = DeviceSerializer(device, data=request.data, partial=True) if serialized.is_valid(): serialized.save() return Response(serialized.data) @@ -113,8 +110,7 @@ def post(self, request: Request) -> Response: request: Http POST request. return: Http JSON response. """ - data = JSONParser().parse(request) - serialized = GrafanaDashboardSerializer(data=data) + serialized = GrafanaDashboardSerializer(data=request.data) if serialized.is_valid(): serialized.save() return Response(serialized.data, status=201) @@ -155,9 +151,8 @@ def patch(self, request: Request, uid: str) -> Response: return: Http JSON response. """ dashboard = self._get_dashboard(uid) - data = JSONParser().parse(request) serialized = GrafanaDashboardSerializer( - dashboard, data=data, partial=True + dashboard, data=request.data, partial=True ) if serialized.is_valid(): serialized.save() @@ -194,8 +189,7 @@ def post(self, request: Request) -> Response: request: Http POST request. return: Http JSON response. """ - data = JSONParser().parse(request) - serialized = FoxgloveDashboardSerializer(data=data) + serialized = FoxgloveDashboardSerializer(data=request.data) if serialized.is_valid(): serialized.save() return Response(serialized.data, status=201) @@ -236,9 +230,8 @@ def patch(self, request: Request, uid: str) -> Response: return: Http JSON response. """ dashboard = self._get_dashboard(uid) - data = JSONParser().parse(request) serialized = FoxgloveDashboardSerializer( - dashboard, data=data, partial=True + dashboard, data=request.data, partial=True ) if serialized.is_valid(): serialized.save()