Pavel Rodionov
Pavel Rodionov

Reputation: 53

SPA Django-Rest-Framework and React Issue: Not Found

I am currently making simple single page application using react and drf. So in drf I've got APIView with post and get methods. I'm fetching the data at my frontend. And it goes all well with get request. But I am stuked with post.

[20/Apr/2018 13:59:11] "GET /api/v1/update/ HTTP/1.1" 200 454
Not Found: /api/v1/update
[20/Apr/2018 13:59:11] "POST /api/v1/update HTTP/1.1" 404 2421`

Help me find the bug please

Here comes my view:

class TaskFormView(APIView):

def get(self, request, format=None):
    """
    Return a list of all tasks.
    """
    tasks = [{'name':task.name, 'status':task.status} for task in Task.objects.all()]
    return Response(tasks)


@csrf_exempt
def post(self, request, format=None):
    '''
    Post a new tasks
    '''
    serializer = TaskSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data, status.HTTP_201_CREATED)
    return Response(serializer.errors, status.HTTP_400_BAD_REQUEST)

And fetch from react app:

  uploadTask() {
fetch("http://127.0.0.1:8000/api/v1/update", {
method: 'POST',
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
  name: "name",
  status: true
})})
.then(response => response.json())
.then(data => console.log('Data is ok. ', data))
.catch(err => console.log("parsing failed", err))
}

Upvotes: 0

Views: 172

Answers (1)

Denys Kotsur
Denys Kotsur

Reputation: 2599

Try to add slash (/) to your POST request.

So, you URL for POST should looks like http://127.0.0.1:8000/api/v1/update/

Upvotes: 1

Related Questions