0

In the Django-Rest-Framework API, I can not use post for testing, I don't know why there is no the post fields.

enter image description here

enter image description here


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'),
]

2 Answers 2

1

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.

0

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.

2
  • This is not the correct answer to the question. The APIView does not implement the get method by default and therefore won't be able to receive a GET request in order to list the fields. On the example you provided, a get method is implemented. Aug 23, 2019 at 13:35
  • Fair, OP is asking about POST though. I link to the example docs for guidance as I'm not sure the question asked is actual the question.
    – ic_fl2
    Aug 23, 2019 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.