Reputation: 26896
In the Django-Rest-Framework API, I can not use post for testing, I don't know why there is no the post fields.
My View:
class WHMCSPhysicalServerIPMIManagementAPIView(APIView):
serializer_class = WHMCSPhysicalServerIPMIManageSerializer
permission_classes = [IsAdminUser]
def post(self, request):
"""
:param request:
action:
physical_server_name:
:return:
"""
try:
physical_server_name = request.query_params.get('physical_server_name')
action = request.query_params.get('action')
if not physical_server_name or not action:
return Response(data="invalid request data", status=HTTP_400_BAD_REQUEST)
physical_server = PhysicalServer.objects.get(name=physical_server_name)
msg = ipmi_management_handler({'action': action, 'physical_server_id': physical_server.id})
return Response(data=msg, status=HTTP_200_OK)
except Exception as e:
return_data = "fail" if e.args == () else e.args[0]
return Response(data=return_data, status=HTTP_200_OK)
My serializers:
class WHMCSPhysicalServerIPMIManageSerializer(Serializer):
physical_server_name = serializers.IntegerField(write_only=True)
action = serializers.CharField(write_only=True)
whmcs_user_id = serializers.IntegerField(write_only=True)
My url:
urlpatterns = [
url(r'^whmcs/physicalserver/ipmi_management/$', WHMCSPhysicalServerIPMIManagementAPIView.as_view(), name='whmcs-physicalserver-ipmi_management'),
]
Upvotes: 0
Views: 1424
Reputation: 1009
I think there is a bit of confusion here. The POST data is in request.data
/ request.post
.
For a simple example have a look at the docs.
Upvotes: 0
Reputation: 2046
You're using an APIView
and you didn't implement the GET
method, so DRF won't be able to render the API interface with the fields you want to post. It'd be appropriate for you to use CreateAPIView
for that.
Upvotes: 1