简体   繁体   English

如何在请求中传递/引用字典

[英]How to pass pass/reference a dictionary in a request

I have made an API call with the function below that I am testing on Postman.我已经使用下面的 function 进行了 API 调用,我正在 Postman 上进行测试。 It takes in an id(dictionary) and deletes the doctor with that id.它接受一个 id(dictionary) 并删除具有该 id 的医生。 So the solution works as it is.Problem is, the id is hard coded - id = {"id": 11}.How do I reference it so that I can feed it in postman instead of hard coding?所以解决方案按原样工作。问题是,id 是硬编码的 - id = {"id": 11}。我如何引用它以便我可以在 postman 中输入它而不是硬编码? I have tried using id = request.GET.get("id") and adding it as a parameter but it didn't work for me.Any assistance will be highly appreciated我尝试使用id = request.GET.get("id")并将其添加为参数,但它对我不起作用。任何帮助将不胜感激

Views.py视图.py

class DoctorBillingPayments(GenericAPIView):
    authentication_classes = [TokenAuthentication]
    permission_classes = [IsAuthenticated]
    @classmethod
    @encryption_check
        def delete(self, request, *args, **kwargs):
            id = {"id": 11}
            try:
                result = {}
                auth = cc_authenticate()
                res = deleteDoctor(auth["key"],id)
                result = res
                return Response(result, status=status.HTTP_200_OK)
            except Exception as e:
                error = getattr(e, "message", repr(e))
                result["errors"] = error
                result["status"] = "error"

            return Response(result, status=status.HTTP_400_BAD_REQUEST)

api_service.py api_service.py

def deleteDoctor(auth, data):
try:
    
    headers = {
                "Authorization": f'Token {auth}'
    }
    url = f'{CC_URL}/doctors/'
    print(url)
    res = requests.delete(url, json=data, headers=headers)

    return res.json()

except ConnectionError as err:
    print("connection exception occurred")
    print(err)

    return err        

I'd recommend using a REST style here and passing the id through to the view using a path argument, like so:我建议在此处使用 REST 样式并使用路径参数将 id 传递给视图,如下所示:

  • DELETE /doctor/{id}

To do so, you'd define the parameter inside your urls :为此,您需要在urls中定义参数:

url(r'^doctor/(?P<id>[0-9]+)/$', DoctorBillingPayments.as_view(), name="doctors")

then you can access it from kwargs for an example request like DELETE /doctor/123然后您可以从kwargs访问它以获取DELETE /doctor/123之类的示例请求

class DoctorBillingPayments(GenericAPIView):
    authentication_classes = [TokenAuthentication]
    permission_classes = [IsAuthenticated]

    @classmethod
    @encryption_check
    def delete(self, request, *args, **kwargs):
        id = int(kwargs['id'])
        try:
            result = {}
            auth = cc_authenticate()
            res = deleteDoctor(auth["key"],id)
            result = res
            return Response(result, status=status.HTTP_200_OK)
        except Exception as e:
            error = getattr(e, "message", repr(e))
            result["errors"] = error
            result["status"] = "error"

        return Response(result, status=status.HTTP_400_BAD_REQUEST)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM