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

400 bad request when connecting android to django-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

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
https://i.stack.imgur.com/zoB40.png

This is what my Django API looks like:
https://i.stack.imgur.com/c1ueV.png

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();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        clientBuilder.addInterceptor(loggingInterceptor);
        /*Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(DjangoApi.DJANGO_SITE)
                .client(clientBuilder.build())

                .addConverterFactory(GsonConverterFactory.create())
                .build();
*/
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(DjangoApi.DJANGO_SITE)
                .client(clientBuilder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        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>() {
            @Override
            public void onResponse(Call<RequestBody> call, Response<RequestBody> response) {
                Log.d("good", "good");

            }
            @Override
            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 = "http://192.168.0.102:8000/image/";

    @Multipart
    @POST("upload/")
    Call<RequestBody>  uploadFile(@Part MultipartBody.Part file);

}
How to&Answers: