So I might be doing something stupid, since I haven't seen anyone complain about this particular issue. So I'm not sure if this is a bug or not.
I'm trying to capture the response from a DELETE request
I've defined the following in my ZoteroService class.
@DELETE("/{type}/{id}/items")
Response deleteItems(@Path(TYPE) LibraryType type,
@Path(ID) long id,
@Query("itemKey") List<String> keyList);
I try and invoke it using
RequestInterceptor authorizingInterceptor = new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
request.addHeader(HttpHeaders.AUTHORIZATION, HttpHeaders.AUTHORIZATION_BEARER_X + credentials.getAccessKey());
request.addHeader(HttpHeaders.ZOTERO_API_VERSION, "3");
}
};
zoteroService = adapter.create(ZoteroService.class);
Response response = zoteroService.deleteItems(type, credentials.getUserID(), keyList);
It makes the following requests
---> HTTP DELETE https://api.zotero.org/users/2351631/items?itemKey=2D3KJIDN
Authorization: Bearer <REDACTED>
Zotero-API-Version: 3
---> END HTTP (no body)
<--- HTTP 204 https://api.zotero.org/users/2351631/items?itemKey=2D3KJIDN (1118ms)
Date: Wed, 11 Mar 2015 22:00:47 GMT
Server: Apache/2.4.10 (Amazon)
Zotero-API-Version: 3
Last-Modified-Version: 154
Vary: Accept-Encoding
Content-Type: text/html; charset=UTF-8
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
OkHttp-Selected-Protocol: http/1.1
OkHttp-Sent-Millis: 1426111248213
OkHttp-Received-Millis: 1426111248623
Then I immediately get a stack trace
retrofit.RetrofitError: unexpected end of stream
at retrofit.RetrofitError.networkError(RetrofitError.java:27)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:395)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at com.sun.proxy.$Proxy4.deleteItems(Unknown Source)
at com.gimranov.libzotero.ZoteroClient.deleteItems(ZoteroClient.java:117)
at com.gimranov.libzotero.ZoteroClientTests.tearDown(ZoteroClientTests.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.net.ProtocolException: unexpected end of stream
at com.squareup.okhttp.internal.http.HttpConnection$FixedLengthSource.read(HttpConnection.java:411)
at okio.RealBufferedSource.read(RealBufferedSource.java:50)
at okio.RealBufferedSource.request(RealBufferedSource.java:71)
at okio.RealBufferedSource.require(RealBufferedSource.java:64)
at okio.GzipSource.consumeHeader(GzipSource.java:114)
at okio.GzipSource.read(GzipSource.java:73)
at okio.RealBufferedSource$1.read(RealBufferedSource.java:298)
at java.io.InputStream.read(InputStream.java:101)
at retrofit.Utils.streamToBytes(Utils.java:43)
at retrofit.Utils.readBodyToBytesIfNecessary(Utils.java:81)
at retrofit.RestAdapter.logAndReplaceResponse(RestAdapter.java:483)
at retrofit.RestAdapter.access$500(RestAdapter.java:107)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:338)
... 30 more
When I manually curl the request, I get the following response from the server.
< HTTP/1.1 204 No Content
< Date: Wed, 11 Mar 2015 20:55:54 GMT
< Server: Apache/2.4.10 (Amazon)
< Zotero-API-Version: 3
< Last-Modified-Version: 135
< Content-Length: 0
< Content-Type: text/html; charset=UTF-8
< Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
Could it have something do with my Content-Length being 0? Does GSON require there to be a body or something?
Upon further investigation of this issue and reaching out to the square/okhttp team (square/okhttp/issues/1490), it appears the issue is caused server side. The server is providing a Content-Length of 20 with a 204 response when the the Content-Encoding is gzipped. e.g.:
< HTTP/1.1 204 No Content
< Date: Thu, 12 Mar 2015 21:03:49 GMT
< Server: Apache/2.4.10 (Amazon)
< Zotero-API-Version: 3
< Last-Modified-Version: 175
< Content-Encoding: gzip
< Vary: Accept-Encoding
< Content-Length: 20
< Content-Type: text/html; charset=UTF-8
< Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
I'm closing this bug as it's not a retrofit bug.
the square/okhttp team
We're the same team!
Welp, that's only a little embarrassing.
I also facing the same issue. But not able to rectify it.
As far as I know, This is a server side issue. Not from Retrofit.
Most helpful comment
We're the same team!