I'm trying to replicate a jpg file upload using OKHttp to a multipart form on a PHP server. I believe, I got some of the parameters in the wrong place, I don't have the familiarity with multipart forms in http and the nomenclature.
Here's what I'm trying to accomplish
Post the parameters (name value pairs): myuser, token, types to https://www.somesite.com/jpgphotoupload.php
I'm then making a multipart form request with method POST, with the following: path: https://www.somesite.com/jpgphotoupload.php
file data: JPEG compressed image data of size 480 x 640 (This I understand)
mimeType: image/jpeg (This I understand)
Not sure where the following name value pairs should be placed as part of the multipart form request, tried addFormDataPart
parameters: again the parameter form above, (myuser, token, types)
name: imagefile
fileName: myname.jpg
In addition, here's what else may be pertinent
"Connection" , "Keep-Alive"
"ENCTYPE", "multipart/form-data"
"Content-Type", "multipart/form-data"
Here's the code that I have currently.
MediaType MEDIA_TYPE_JPG = MediaType.parse("image/jpg");
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new MultipartBuilder()
.type(MultipartBuilder.FORM)
.addPart(
Headers.of("Content-Disposition", "form-data; name=\"imagefile\""),
RequestBody.create(MEDIA_TYPE_JPG, new File("/storage/emulated/0/download/camerapic.jpg")))
.addFormDataPart("myuser", getprefmyuser(getBaseContext()))
.addFormDataPart("token", getpreftoken(getBaseContext()))
.addFormDataPart("types", "type1")
.addFormDataPart("fileName", "myname.jpg")
.build();
Request request = new Request.Builder()
.header("myuser", getprefmyuser(getBaseContext()))
.header("token", getpreftoken(getBaseContext()))
.header("type", "car")
.url("https://www.somesite.com/jpgphotoupload.php")
.post(requestBody)
.build();
Response response = null;
try {
response = client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
...
return null;
}