djangoajaxdjango-rest-frameworkbackendthunk

how to make thunk HTTP POST request to Django rest api


I'm pretty new to this, so I will show you the error first and then display my thought process.

I am attempting to use a thunk (Http request in redux) to make a post request to my personal rest api in django. (so it can run cURL commands and use a authorization code to gain an access token and get user data from an API) when I make this request, the network returns a 201 status to stay it worked, while my console logs the dispatch as rejected, and gives the error, "message: "Unexpected end of JSON input". In this message, payload is undefined, and my payload is living in meta under arg.

After much reserach, it looks like I either need to JSON.strigify something on my front end, OR that django is receiving a null object and not knowing what to do with it. I believe it is the latter, and not sure how to proceed. Here is my code:

FRONTEND: slice.py

    import {createAsyncThunk} from '@reduxjs/toolkit'
        
    export const postRequest= createAsyncThunk('slice/postRequest',
          async postData => {
            const response=
            await client.post('url', {slice:postData})
            return response.slice
          }
        )

component.py

  import {useDispatch} from 'react-redux'
  import {postRequest} from './slice'

  const Component = () => {
      const dispatch=useDispatch()
      const authCode= 'samplecode'
      const data={authCode:`${authCode}`, userData:{}}
      dispatch(postRequest(JSON.stringify(data))

     return(null)
 }

export default Component

BACKEND:

models.py from django.db import models

    class UserData(models.Model):
      authCode=models.CharField(max_length=100, default='')
      userData= models.JSONField

serializers.py

from rest_framework import serializers
from .models import *

class UserDataSerializer(serializers.ModelSerializer):
  class meta:
    model=UserData
    fields= ('authCode', 'userData')

views.py

from .models import *
from .serializers import *
from django.views.decorators.csrf import csrf_excempt
from rest_framework.decorators import api.view
from rest_framework.resposne import Response
from rest_framework import status

@api_view(['POST'])
@csrf_excempt
define restapi(request)
  if request.method == "POST":
    serializer = UserDataSerializer(data = request.data
    if serializer.is_valid():
      serializer.save
      return Response(status=status.HTTP_201_CREATED)
    return Response(serializer.errors, status=status.HTTP_400_BAD_Request

I also tried it where it would return a status of 204, and without JSON.stringify. I noticed that when I made a successful post request to a json server, it would make the request with the payload in args, and if it succeeded, the payload would get moved to payload, but it is my guess that my current error is happening because the payload is blank at the start of the thunk dispatch. Really I am not sure though


Solution

  • I solved this problem, it was stupid.

    I was using a homemade client and I am not experienced enough to do that. I just got axios and used that as the client and it worked fine. I also simplified the request in the component to not have JSON.stringify and not have the backticks.