0

I'm trying to send one image and somes string through retrofit, but it's not working, I'm always getting a 400. My app is already using webservices such as GET or POST without troubles, I assume that my probleme is with the upload...

I used that explanations and the github of retrofit.

My idea here is to create a new Dish and sed it to my server, so most of the data are comming from InputTextField and the photo is taken by the camera using an Intent

First my interface for uploading

@Multipart
@POST("japronto/api/chef/dish/new")
Call<Dish> upload(@Part("name") RequestBody name, @Part("description") RequestBody description,@Part("disponibility") RequestBody disp,@Part("max") RequestBody max,@Part("price") RequestBody price, @Part MultipartBody.Part file);

My function to upload

    public void onOK(){

    RequestBody name = RequestBody.create( MediaType.parse("multipart/form-data"), this.name.getText().toString());
    RequestBody description =
            RequestBody.create(
                    MediaType.parse("multipart/form-data"), this.description.getText().toString());
    RequestBody disp =
            RequestBody.create(
                    MediaType.parse("multipart/form-data"), Integer.toString(this.TorF));

    RequestBody max =
            RequestBody.create(
                    MediaType.parse("multipart/form-data"), this.max.getText().toString());

    RequestBody price =
            RequestBody.create(
                    MediaType.parse("multipart/form-data"), this.price.getText().toString());


    File nImg = new File(folderImg, imgName);

    RequestBody rqFile =
            RequestBody.create(MediaType.parse("multipart/form-data"), nImg);

    MultipartBody.Part body =
            MultipartBody.Part.createFormData("picture", nImg.getName(), rqFile);

    ApiService apiService = ApiManager.createService(ApiService.class, this.chef.getPseudo(), this.chef.getPassword());
    Call<Dish> call = apiService.upload(name, description, disp, max, price, body);
    call.enqueue(new Callback<Dish>() {
        @Override
        public void onResponse(Call<Dish> call, Response<Dish> response) {
            Dish d = response.body();
            Log.d(TAG, "onResponse: "+d.getName());
        }

        @Override
        public void onFailure(Call<Dish> call, Throwable t) {

        }
    });

}

My view on the server

    @app.route('/japronto/api/chef/dish/new', methods=['POST'])
    def upload():
    if request.method == 'POST':
     if 'file' not in request.files:
        print 'pbs'
    file = request.files['file']

    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        print filename
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        return {'name':filename}
return{'name':'erro'}

Any ideas?

Darakt
  • 15
  • 6

1 Answers1

0

Try this

@Multipart
@POST("japronto/api/chef/dish/new")
Call<Dish> upload(@Part("name") RequestBody name, @Part("description") RequestBody description,@Part("disponibility") RequestBody disp,@Part("max") RequestBody max,@Part("price") RequestBody price, @Part("ImageNameHere\"; filename=\"image.png\" ") RequestBody image);



File nImg = new File(folderImg, imgName);
RequestBody rqFile = RequestBody.create(MediaType.parse("image/png"), nImg);

check these links

Link 1 Link 2 Link 3

Community
  • 1
  • 1
Samar Ali
  • 373
  • 5
  • 13
  • There is some progress, now my server is giving an error 400 and I still can't get the image doing an `file = request.files['file'] ` (using Flask) – Darakt Oct 25 '16 at 17:43
  • try hitting same url with postman and see if problem is from server code or app code – Samar Ali Oct 26 '16 at 07:24
  • Already tried and I´ve got no error... I can´t test right now, but I think that the probleme is in the header of the packet. – Darakt Oct 27 '16 at 00:12