title: How to send a multipart request in Android with Volley
body: <p>I have some code to send multipart request with Android Volley. But When i'm try running the code, showing error "BasicNetwork.performRequest: Unexpected response code 401". </p>
  
  <p>Hope, anyone can help me. Thanks.</p>
  
  <p>This My Multipartrequest.</p>
  
  <pre><code>public class MultipartRequest extends Request&lt;String&gt; {
    MultipartEntityBuilder entity = MultipartEntityBuilder.create();
    HttpEntity httpentity;
    private String FILE_PART_NAME = "upload";
  
    private final Response.Listener&lt;String&gt; mListener;
    private final File mFilePart;
    private final Map&lt;String, String&gt; mStringPart;
  
  public MultipartRequest(String url, Response.ErrorListener errorListener,
                          Response.Listener&lt;String&gt; listener, File file,
                          long length, Map&lt;String, String&gt; mStringPart, HashMap&lt;String, String&gt; headers, HashMap&lt;String, String&gt; params, Object o) {
      super(Method.POST, url, errorListener);
  
      this.mListener = listener;
      this.mFilePart = file;
      this.mStringPart = mStringPart;
  
      entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
      try {
          entity.setCharset(CharsetUtils.get("UTF-8"));
      } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
      }
      buildMultipartEntity();
      httpentity = entity.build();
  }
  
  private void buildMultipartEntity() {
      entity.addPart(FILE_PART_NAME, new FileBody(mFilePart, ContentType.create("image/jpeg"), mFilePart.getName()));
      if (mStringPart != null) {
          for (Map.Entry&lt;String, String&gt; entry : mStringPart.entrySet()) {
              entity.addTextBody(entry.getKey(), entry.getValue());
          }
      }
  }
  
  @Override
  public String getBodyContentType() {
      return httpentity.getContentType().getValue();
  }
  
  @Override
  public byte[] getBody() throws AuthFailureError {
  
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      try
      {
          httpentity.writeTo(bos);
      }
      catch (IOException e)
      {
          VolleyLog.e("IOException writing to ByteArrayOutputStream");
      }
      return bos.toByteArray();
  }
  
  @Override
  protected Response&lt;String&gt; parseNetworkResponse(NetworkResponse response) {
  
      try {
          System.out.println("Network Response "+ new String(response.data, "UTF-8"));
          return Response.success(new String(response.data, "UTF-8"),
                  getCacheEntry());
      } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
          return Response.success(new String(response.data), getCacheEntry());
      }
  }
  
  @Override
  protected void deliverResponse(String response) {
      mListener.onResponse(response);
  }
  }
  </code></pre>
  
  <p>And i call multipart request like this.</p>
  
  <pre><code> private void upload(){
      HashMap&lt;String, String&gt; headers = new HashMap&lt;String, String&gt;();
      String credentials = AppConfig.USER_API+":"+AppConfig.PASSWORD_API;
      String auth = "Basic "
              + Base64.encodeToString(credentials.getBytes(),
              Base64.NO_WRAP);
      headers.put("Authorization", auth);
  
      File sourceFile = new File(filePath);
  
      HashMap&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
      params.put("desc", "OK");
  
      MultipartRequest multipartRequest = new MultipartRequest(AppConfig.URL_REPORT,
  
              new Response.ErrorListener() {
                  @Override
                  public void onErrorResponse(VolleyError error) {
                      Log.e(TAG, "Failed : " + error.getMessage());
                      Toast.makeText(getApplicationContext(),
                              error.getMessage(), Toast.LENGTH_LONG).show();
                  }
  
              },
              new Response.Listener&lt;String&gt;() {
                  @Override
                  public void onResponse(String response) {
                      Toast.makeText(getApplicationContext(),
                              "Success", Toast.LENGTH_LONG)
                              .show();
                  }
  
              }, sourceFile, sourceFile.length(), null, headers, params, null);
      VolleyController.getInstance().addToRequestQueue(multipartRequest);
  }
  </code></pre>