Home > Enterprise >  how to pass data in body of API request
how to pass data in body of API request

Time:12-09

I need to update some info in my database using PUT request and Django rest framework

I need to update max_date parameter by passing the value in the body of the query (pass True or False) but i have 0 ideas how to do it

i only know how to update something by passing a variable in the url, like api/config/max_date/<int:boolean_by_number>

my URLs:

urlpatterns_config = [
    path('all/', api_config.APIListConfig.as_view(), name='api info config'),
    path('forever/', api_config.APIForever.as_view(), name='put or get forever config'),
    path('max_date/', api_config.APIMaxDate.as_view(), name='put or get max_date config')
]

my API file:

class APIMaxDate(APIView):

    def get(self, request, *args, **kwargs) -> JsonResponse:

        config = Configuration.objects.get(name='max_date')

        data = {
            f"{config.name}": config.value
        }
    
        return JsonResponse(data, status=status.HTTP_200_OK)

    def put(self, request, *args, **kwargs) -> JsonResponse:

        return JsonResponse({}, status=status.HTTP_201_CREATED)

CodePudding user response:

If values are passed via PUT, you can access them in django like so:

max_date = request.PUT.get('max_date', False)
if max_date:
    # continue to use max_date to update your db instance
  • Related