title: Retrofit 2 Multipart image upload with data
body: <p>Hello everyone I want to post image and other data through Retrofit2.
  I am sending data with one image. </p>
  
  <blockquote>
    <p>All the other info is storing but my image is not storing.while i am testing with postman, it works.</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/vRlzh.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/vRlzh.jpg" alt="enter image description here"></a></p>
  
  <p>please guide me where I am lacking in my code</p>
  
  <p>This is the postman code snippet that works</p>
  
  <pre><code>OkHttpClient client = new OkHttpClient();
  
  MediaType mediaType = MediaType.parse("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
  RequestBody body = RequestBody.create(mediaType, "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"email\"\r\n\r\ntest6@gmail.com\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"password\"\r\n\r\n123456\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\nTest\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"phone\"\r\n\r\n1234567890\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"image\"; filename=\"03.JPG\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--");
  Request request = new Request.Builder()
    .url("https://"url"/api/v1/sign-up")
    .post(body)
    .addHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW")
    .addHeader("cache-control", "no-cache")
    .addHeader("postman-token", "2dd038d9-5f52-fcd0-9331-445eaf35c230")
    .build();
  
  Response response = client.newCall(request).execute();
  </code></pre>
  
  <p>Below is the postman request image:</p>
  
  <p><a href="https://i.stack.imgur.com/cUOUy.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/cUOUy.jpg" alt="enter image description here"></a></p>
  
  <p>this is my Retrofit api</p>
  
  <pre><code>    @Multipart
      @POST("sign-up")
      Call&lt;SignUpResponse&gt; getSignUpResponse(
              @Part("email") RequestBody email,
              @Part("password") RequestBody password,
              @Part("name") RequestBody name,
              @Part("phone") RequestBody phone,
              @Part MultipartBody.Part image
              //@Part("image") RequestBody image // i have thried them both but they didnt work
              //@Part("image\"; filename=\"pp.jpg\" ") RequestBody image
      );
  </code></pre>
  
  <p>this is my client area: </p>
  
  <pre><code>private RetrofitClient() {
  
      HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
      logging.setLevel(HttpLoggingInterceptor.Level.BODY);
      OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
      httpClient.addInterceptor(logging);  // &lt;-- this is the important line!
  
      retrofit = new Retrofit.Builder()
              .baseUrl(BASE_URL)
              .addConverterFactory(GsonConverterFactory.create())
              .client(httpClient.build())
              .build();
  }
  </code></pre>
  
  <p>and this is the part where i am making the request: </p>
  
  <pre><code>RequestBody namePart = RequestBody.create(MultipartBody.FORM, "nameasd");
  RequestBody emailPart = RequestBody.create(MultipartBody.FORM, "emailasd@gmai.com");
  RequestBody mobilePart = RequestBody.create(MultipartBody.FORM, "123456623");
  RequestBody passwordPart = RequestBody.create(MultipartBody.FORM, "123456123");
  //String filepath = "/storage/0403-0201/DCIM/Camera/20180926_203219.jpg"; this is the image source
  File file = new File(filepath);
  RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
  //RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
  MultipartBody.Part body = MultipartBody.Part.createFormData("image",file.getName(),reqFile);
  
  Call&lt;SignUpResponse&gt; call = RetrofitClient.getInstance().getApi().getSignUpResponse(emailPart, passwordPart, namePart, mobilePart, body);
                  call.enqueue(new Callback&lt;SignUpResponse&gt;() {
                      @Override
                      public void onResponse(Call&lt;SignUpResponse&gt; call, Response&lt;SignUpResponse&gt; response) {
                          progressDialog.dismiss();
                          Log.d(TAG, "onResponse: "+response.body());
                          Log.d(TAG, "onResponse: meta: " + response.body().getMeta().getStatus());
                      }
                      @Override
                      public void onFailure(Call&lt;SignUpResponse&gt; call, Throwable t) {
                          Toast.makeText(SignupActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
                          Log.d(TAG, "onFailure: "+t.getMessage());
                      }
                  });
  </code></pre>
  
  <p>this is the code where i get the data</p>
  
  <pre><code>@Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          // get selected images from selector
          if (requestCode == REQUEST_CODE) {
              if (resultCode == RESULT_OK) {
  
                  mResults = data.getStringArrayListExtra(SelectorSettings.SELECTOR_RESULTS);
                  imagePath = mResults.get(0);
                  Glide.with(SignupActivity.this)
                          .load(mResults.get(0))
                          .into(profileImage);
                }
          }
          super.onActivityResult(requestCode, resultCode, data);
      }
  </code></pre>
  
  <blockquote>
    <p>I even set it on a view and that works...</p>
  </blockquote>