title: Retrofit 2 RequestBody Content Length Greater Than File Size
body: <p>I have an Api which requires me to send up the size of a file in bytes before I then send up the actual file. When I call <code>file.length()</code> on the file to send up in the first Api call, it returns <code>1996</code>. </p>
  
  <p>When I then package the file up into a <code>RequestBody</code> the <code>contentLength()</code> becomes <code>2556</code>. The server then rejects this, saying that these sizes must match.</p>
  
  <p>Here is the code for creating the <code>RequestBody</code>.</p>
  
  <pre><code>val requestBody = RequestBody.create(
                          MediaType.parse("image/jpeg"),
                          avatarFile)
  </code></pre>
  
  <ul>
  <li>What is being added in the <code>RequestBody</code> to increase its content length? </li>
  <li>Should I just send up the size of the <code>RequestBody</code> on the first call to get around this problem?</li>
  </ul>
  
  <p><strong>EDIT</strong> </p>
  
  <p>Here is where I call the API the first time to send the size initially:</p>
  
  <pre><code>return authService.updateAvatar(
                      AvatarMetadataRequest(
                              size = avatarFile.length().toInt(),
                              crc = profileImageProvider.getFile()!!.checksum()!!.toInt()))
  </code></pre>
  
  <p>And then this is where I call it the second time when the file actually gets uploaded:</p>
  
  <pre><code>        val avatarFile = profileImageProvider.getFile()
  
          val requestBody = RequestBody.create(
                  MediaType.parse("image/jpeg"),
                  avatarFile)
          return authService.uploadAvatar(
                  id.split("/").last(),
                  MultipartBody.Part.createFormData("avatar",
                          profileImageProvider.getFileName(),
                          requestBody))
  </code></pre>
  
  <p>It turns out the <code>contentLength()</code> of the <code>requestBody</code> is correct. So the <code>Content-Length</code> must be being modified or increased when the <code>MultipartBody.Part.createFormData</code> is called.</p>