Django POST endpoint receives empty data when sending text via axios

I’m having trouble sending a simple text string from my React frontend to a Django view using axios POST requests.

Frontend code:

const httpClient = axios.create({
  baseURL: 'http://localhost:8000',
  headers: {
    'Content-Type': 'application/json'
  }
});

class ChatAPI {
  static async submitQuery(userInput) {
    const payload = { text: userInput };
    const result = await httpClient.post('/api/process_input', payload);
    return result;
  }
}

Django view:

@csrf_exempt
def process_input(request):
    print("Received POST request")
    raw_data = request.body
    decoded_data = raw_data.decode('utf-8')
    print("Data received:", decoded_data)
    return JsonResponse({'status': 'ok'})

The Django view executes but decoded_data is always an empty string. The browser shows the request went through but no payload data reaches the backend. What could be causing this issue?

maybe the axios call needs to be form-data instead. try adjusting ur headers or check if there’s middleware blocking it. also, double-check the url path for the endpoint.

Your Django view isn’t parsing the JSON properly. You’re decoding request.body but not actually parsing the JSON data. Here’s the fix:

import json

@csrf_exempt
def process_input(request):
    print("Received POST request")
    raw_data = request.body
    decoded_data = raw_data.decode('utf-8')
    json_data = json.loads(decoded_data)
    print("Data received:", json_data)
    user_text = json_data.get('text', '')
    return JsonResponse({'status': 'ok'})

You could also use request.POST if you switch your frontend to send form data instead, but since you’re already setting Content-Type to application/json, parsing JSON is the right way to go.

hmm, that’s a puzzling issue! i wonder if it’s related to CORS or some middleware. did u check the network tab for the payload? also, logging request.POST might reveal more. got any ideas on what could be causing this?