Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Im finishing this project which is using okhttp for communication with a webservice.

All is going fine for regular GETs and POSTs, but I'm not being able to properly upload a file.

The okhttp docs are very lacking on these subjects and everything I found here or anywhere don't seem to work in my case.

It's supposed to be simple: I have to send both the file and some string values. But I can't figured out how to do it.

Following the some samples I found, I first tried this:

RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM)
    .addFormDataPart("group", getGroup())
    .addFormDataPart("type", getType())
    .addFormDataPart("entity", Integer.toString(getEntity()))
    .addFormDataPart("reference", Integer.toString(getReference()))
    .addPart(Headers.of("Content-Disposition", "form-data; name="task_file""), RequestBody.create(MediaType.parse("image/png"), getFile()))
    .build();

It gives me a "400 bad request" error.

So I tried this from the okhttp recipes:

RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM)
    .addPart(Headers.of("Content-Disposition", "form-data; name="group""), RequestBody.create(null, getGroup()))
    .addPart(Headers.of("Content-Disposition", "form-data; name="type""), RequestBody.create(null, getType()))
    .addPart(Headers.of("Content-Disposition", "form-data; name="entity""), RequestBody.create(null, Integer.toString(getEntity())))
    .addPart(Headers.of("Content-Disposition", "form-data; name="reference""), RequestBody.create(null, Integer.toString(getReference())))
    .addPart(Headers.of("Content-Disposition", "form-data; name="task_file""), RequestBody.create(MediaType.parse("image/png"), getFile()))
    .build();

Same result.

Don't know what else to try or what look into to debug this.

The request is done with this code:

// adds the required authentication token
Request request = new Request.Builder().url(getURL()).addHeader("X-Auth-Token", getUser().getToken().toString()).post(requestBody).build();
Response response = client.newCall(request).execute();

But Im pretty sure that the problem is how Im building the request body.

What am I doing wrong?

EDIT: "getFile()" above returns the a File object, by the way. The rest of the parameters are all strings and ints.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
154 views
Welcome To Ask or Share your Answers For Others

1 Answer

I found answer to my own question a bit after initial post.

I′ll leave it here, because it can be useful to others, given that there is such a few okhttp upload examples around:

RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM)
        .addFormDataPart("group", getGroup())
        .addFormDataPart("type", getType())
        .addFormDataPart("entity", Integer.toString(getEntity()))
        .addFormDataPart("reference", Integer.toString(getReference()))
        .addFormDataPart("task_file", "file.png", RequestBody.create(MediaType.parse("image/png"), getFile()))
                                                .build();

There is no reason to use "addPart" with "Headers.of" etc like in the recipes, addFormDataPart does the trick.

And for the file field itself, it takes 3 arguments: name, filename and then the file body. That's it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...