alwayswill / google-api-java-client

Automatically exported from code.google.com/p/google-api-java-client
0 stars 0 forks source link

zero length error when content payload is empty #234

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Version of google-api-java-client: 1.4.1-beta

Java environment: observed on Java 6

Description:

I send a basic authentication header with my HTTP POST request, and when there 
is a content payload, everything works fine. However, there is one circumstance 
where there is no actual content payload, and 1.4.1-beta is causing a 
"content-length is zero" error to occur when the request is sent. This worked 
fine with 1.3.x, but the way I used to send these requests has been deprecated, 
so I'm trying to do it the 1.4.x way.

How would you expect it to be fixed?

My current solution is to send useless data with these requests, which causes 
the content-length error to go away, but is just covering up either a bug in 
the API or my own misuse of the API.

When someone needs to send a POST request, without content, but with a basic 
auth header, there should not be a "content-length is zero" error.

Here's how I call the API:

private static HttpRequestFactory createRequestFactory() {
  return (new NetHttpTransport()).createRequestFactory(
    new HttpRequestInitializer() {
      public void initialize(final HttpRequest request) 
        throws IOException {
          request.headers.setBasicAuthentication(username, password);
          final JsonHttpParser parser = new JsonHttpParser();
          parser.jsonFactory = new JacksonFactory();
          request.addParser(parser);
      }
    });
  }

public void myFunction() {
  // use FORM POST encoding

  final UrlEncodedContent content = new UrlEncodedContent();
  content.data = requestData;

  // create POST request

  final HttpRequest httpRequest = requestFactory.buildPostRequest(
    new GenericUrl(requestUrl), null);

  // parse return data

  return httpRequest.execute().parseAs(ExpectedResponseData.class);
}

The relevant stacktrace is:

com.google.api.client.http.HttpResponseException: 411 Length required
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:380)
...

Original issue reported on code.google.com by tra...@circutus.com on 16 Jun 2011 at 7:58

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Here's a corrected version of myFunction from the example above.

public void myFunction() 
{
  final HttpRequest httpRequest = createRequestFactory().buildPostRequest(
    new GenericUrl(requestUrl), null);

  return httpRequest.execute().parseAs(ExpectedResponseData.class);
}

The same problems occur when I write it this way, with null content.data.

public void myFunction() 
{
  final UrlEncodedContent content = new UrlEncodedContent();
  content.data = null;

  final HttpRequest httpRequest = createRequestFactory().buildPostRequest(
    new GenericUrl(requestUrl), content);

  return httpRequest.execute().parseAs(ExpectedResponseData.class);
}

Original comment by tra...@circutus.com on 16 Jun 2011 at 8:10

GoogleCodeExporter commented 9 years ago
I'm having the same issue of zero length when trying to upload to youtube with 
a multipart content, I really don't know how to solve it.

Original comment by maurru...@gmail.com on 25 Jun 2011 at 4:00

GoogleCodeExporter commented 9 years ago
I have this problem in the google tasks api when I "Move.execute()" a task.

Original comment by mwb...@gmail.com on 1 Jul 2011 at 10:30

GoogleCodeExporter commented 9 years ago
This appears to have been fixed in 1.5.0-beta (the original issue I posted on).

Original comment by tra...@circutus.com on 1 Oct 2011 at 7:38

GoogleCodeExporter commented 9 years ago
We had an internal investigation of this issue, and have not determined the 
root cause yet.  I'm keeping this open until we do.  Here's a summary what we 
[think we] know so far:

We don't know why, but we noticed this bug reproduces on the Sun JDK but not 
OpenJDK.  So if you are seeing this problem, our recommendation is that if you 
are sending a POST with no content, you should use dummy data like a single 
space (" ").  Our service-specific libraries and MethodOverride do this 
automatically by doing this:

if (request.content == null || request.content.getLength() == 0) {
      request.content = new ByteArrayContent(" ");
}

Original comment by yan...@google.com on 3 Oct 2011 at 11:54