Home » Android » 400 bad request when connecting android to django-Exceptionshub

400 bad request when connecting android to django-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment


I am trying to upload a clicked picture from android to Django API, but I am getting 400 Bad Request when I tried debugging with logging interceptor. I have surfed all the similar posts but none helped.

My Django API works fine when I try uploading an image from local drive, the problem is only when I tried to POST an image from android to backend. It would be very nice if anyone could help me.

This is what my logging interceptor looks like

This is what my Django API looks like:

Django Views.py

from rest_framework.generics import (CreateAPIView)
from image_app.models import MyImage
from django.http import HttpResponse
from rest_framework.response import Response
from rest_framework import viewsets

class ImageCreateAPIView(CreateAPIView):
    queryset = MyImage.objects.all()
    serializer_class = imageSerializer

Method in Main Activity

        public void uploadPicture(File imageFile, Uri uri ){
        System.out.println("The imagefile is stored in "+imageFile);
        OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        /*Retrofit retrofit = new Retrofit.Builder()

        Retrofit retrofit = new Retrofit.Builder()

        Log.d("Django post API", retrofit+"");

        DjangoApi postApi= retrofit.create(DjangoApi.class);

        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), imageFile);
        MultipartBody.Part multiPartBody = MultipartBody.Part
                .createFormData("model_pic", imageFile.getName(), requestBody);

        Call<RequestBody> call = postApi.uploadFile(multiPartBody);

        call.enqueue(new Callback<RequestBody>() {
            public void onResponse(Call<RequestBody> call, Response<RequestBody> response) {
                Log.d("good", "good");

            public void onFailure(Call<RequestBody> call, Throwable t) {
                Log.d("fail", "fail");


Interface of Django API in android end

public interface DjangoApi {

    String DJANGO_SITE = "";

    Call<RequestBody>  uploadFile(@Part MultipartBody.Part file);

How to&Answers: