jagcweb
jagcweb

Reputation: 350

post() missing 1 required positional argument: 'request'

I have a problem with the registry. And it is that when I make the request through postman it throws me the error: post () missing 1 required positional argument: 'request'

My url:

from .views import UserRegistrationView

urlpatterns = [
    path('register', UserRegistrationView.post),
]

Url include:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/user/', include('user.urls')),
] 

My View

from rest_framework import status
from rest_framework.generics import CreateAPIView
from rest_framework.response import Response
from rest_framework.permissions import AllowAny
from .serializers import UserRegistrationSerializer
from rest_framework.decorators import api_view

class UserRegistrationView(CreateAPIView):
    permission_classes = (AllowAny,)

    @api_view(('POST',))
    def post(self, request):
        serializer = UserRegistrationSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        status_code = status.HTTP_201_CREATED
        response = {
            'success' : 'True',
            'status code' : status_code,
            'message': 'User registered  successfully',
            }
        
        return Response(response, status=status_code)

My post by Postman:

url: http://127.0.0.1:8000/api/user/register

Body (raw):

{
 "email":"[email protected]",
 "password":"123456",
 "profile": {
        "first_name": "asdf",
        "last_name": "asdf",
        "phone_number": "622111444",
        "age": 38,
        "gender": "M"
 }
}

My Error: TypeError at /api/user/register post() missing 1 required positional argument: 'request'

Upvotes: 0

Views: 1639

Answers (1)

jagcweb
jagcweb

Reputation: 350

Fixed, the problem came from not having parsed the json.

Solution:

user_data = JSONParser().parse(request)
serializer = UserRegistrationSerializer(data=user_data)

Upvotes: 1

Related Questions