• Register
2 votes
32 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,060 points

Please log in or register to answer this question.

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,950 points

Related questions

1 vote
1 answer 4 views
4 views
Problem: Whgy HTTP method GET is not supported by this URL. Usaa error 405: http method GET is not supported by this URL.
asked Mar 30 PkGuy 13.1k points
0 votes
1 answer 33 views
33 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, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 1K views
1K 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, 2020 jwilliam 3.9k points
0 votes
1 answer 10 views
10 views
Problem: I want help: the get method is not supported for this route. supported methods: post.
asked Mar 19 Wafa Abu Yousef 6.1k points
0 votes
1 answer 3 views
3 views
Problem: Can anybody help me: the get method is not supported for this route. supported methods: post.
asked Mar 17 Wafa Abu Yousef 6.1k points
0 votes
1 answer 25 views
25 views
Problem: There is a database on postgresql. There is a Django script that collects data. The problem is that there is a LOT of data. The table, with which it is necessary to operate, contains, according to conservative estimates, from hundreds of millions ... {}'.format(str(self.origination_host), str(self.termination_host)) class Meta: verbose_name = 'CDR data' verbose_name_plural = 'CDR data'
asked Dec 7, 2020 sasha 16.2k points
0 votes
1 answer 2 views
2 views
Problem: As I previously stated in my question, I am having difficulty learning. Could someone help assist me in completing my project?
asked Mar 30 tuhin1 48.4k points
0 votes
1 answer 67 views
67 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, 2020 maddi86 5.4k points
0 votes
1 answer 307 views
307 views
Problem: Django I have the following models: class Group(models.Model): name = models.CharField(max_length=100) parent_group = models.ManyToManyField("self", blank=True) def __unicode__(self): return self.name class Block(models.Model): name = models.CharField(max_length= ... return blocks But b1.group.block_set returns me a RelatedManager object that cannot be repeated. What to do? What's wrong?
asked Dec 13, 2020 sasha 16.2k points