question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multipart - how to disable Content-Length for a part?

See original GitHub issue

Question: How to disable Content-Length header for a part? I need to do this because an API that I’m using does not allow it (and I can’t do anything about it).

I’m sure that the problem is that header, because I’ve tried to upload a file manually using HttpURLConnection and it works great without Content-Length, and it fails immediately when I add it.

I’ve tried to add interceptor to the request (both addInterceptor and addNetworkInterceptor) in order to remove unwanted header. But it is not here (I can see only Content-Disposition). Is there another way to disable it?

Mentioning https://github.com/square/okhttp/issues/2138

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:8
  • Comments:22 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
swankjessecommented, Jun 10, 2016

You’re the only one. You should petition the owner of the server who has this limitation to fix their server so we don’t have to fix our client.

5reactions
swankjessecommented, Nov 18, 2019

@code-n-roll this might get you unstuck:

  /** Returns a request body with no length available. */
  private RequestBody stripLength(RequestBody delegate) {
    return new RequestBody() {
      @Override public @Nullable MediaType contentType() {
        return delegate.contentType();
      }

      @Override public void writeTo(BufferedSink sink) throws IOException {
        delegate.writeTo(sink);
      }
    };
  }

Use it like this:

    RequestBody part = ...
    MultipartBody body = new MultipartBody.Builder("123")
        .addPart(stripLength(part))
        .build();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Okhttp - Multipart - how to disable Content-Length for a part?
I've tried to add an interceptor to the request (both addInterceptor and addNetworkInterceptor ) in order to remove that unwanted header. But it ......
Read more >
RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1
1. If the Request-URI is encoded using the "% HEX HEX" encoding [42], the origin server MUST decode the Request-URI in order to...
Read more >
Multipart Requests
OAS 3 This guide is for OpenAPI 3.0. Multipart Requests. Multipart requests combine one or more sets of data into a single body,...
Read more >
POST - HTTP - MDN Web Docs
The HTTP POST method sends data to the server. The type of the body of the request is indicated by the Content-Type header....
Read more >
File uploads (profile image, simple, resumable uploads)
You must provide a top-level Content-Length header that declares the length of the entire request body in bytes. · The first part of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found