• Register
2 votes
17 views

Problem:

I am new in django I was trying some code to get and post some data. I wrote a code:
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework import viewsets


from profiles_api import serializers


class HelloApiView(APIView):
    """Test API View"""
    serializer_class = serializers.HelloSerializer
    def post(self, request):
        """Create a hello message with our name"""
        serializer = self.serializer_class(data=request.data)

        if serializer.is_valid():
            name = serializer.validated_data.get('name')
            message = f'Hello {name}!'
            return Response({'message': message})
        else:
            return Response(
                serializer.errors,

But every time I run my server It shows an error message:

message HTTP method POST is not supported by this URL
description The specified HTTP method is not allowed for the requested resource (HTTP method POST is not supported by this URL)

 

Can anyone help?

10 7 2
6,050 points

1 Answer

0 votes

Answer:

For your api you need to call a get method then post or else It would show this error message. So write your code like:

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework import viewsets


from profiles_api import serializers


class HelloApiView(APIView):
    """Test API View"""
    serializer_class = serializers.HelloSerializer

    def get(self, request, format=None):
        """Returns a list of APIView features"""
        an_apiview = [
            'Uses HTTP methods as function (get, post, patch, put, delete) ',
            'Is similar to a tarditional Django View',
            'Gives you the most control over your application logic',
            'Is mapped manually to URLs',
        ]

        return Response({'message': 'Hello!', 'an_apiview': an_apiview })

    def post(self, request):
        """Create a hello message with our name"""
        serializer = self.serializer_class(data=request.data)

        if serializer.is_valid():
            name = serializer.validated_data.get('name')
            message = f'Hello {name}!'
            return Response({'message': message})
        else:
            return Response(
                serializer.errors,
                status=status.HTTP_400_BAD_REQUEST
            )

Happy coding

11 6 4
34,930 points

Related questions

0 votes
1 answer 12 views
12 views
Problem: I am a na&iuml;ve to Java, Jsp, Servlet and trying to make a very small registration program with the database. But while trying to run this I am facing below errors: HTTP Status 405 - HTTP method GET is not supported by this URL type Status report message HTTP ... </welcome-file>     </welcome-file-list> </web-app> I am looking for Java expert who can help me in fixing above error.
asked Jul 29 Raphael Pacheco 4.9k points
0 votes
1 answer 447 views
447 views
Problem : Currently I am trying to add the table to the newly created database through SQL Server Management Studio. But I get the below error: &ldquo;The backend version is not supported to design database diagrams or tables&rdquo; I am very new to SQL Server Management Studio. How can I resolve the above error?
asked Jan 28 jwilliam 3.9k points
0 votes
1 answer 28 views
28 views
Problem: I am new and learning, I need help, can anyone help by filling in the blanks? A field in an Access database table that has no value is determined by Access to have a ____ value? A. custom B. zero C. null D. number
asked Feb 21 maddi86 5.4k points
0 votes
1 answer 40 views
40 views
Problem: A(n) _____ might be written by a programmer or it might be created through a DBMS utility program?
asked Jul 23 conner 1.5k points