Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upmethod POST must have a request body. #854
Comments
This comment has been minimized.
This comment has been minimized.
crossle
commented
May 21, 2015
|
Should the okhttp report error. https://github.com/square/okhttp/blob/master/CHANGELOG.md |
This comment has been minimized.
This comment has been minimized.
RahulRvR
commented
May 21, 2015
|
I am getting the same exception , but currently I have used a workaround as per this SO post |
This comment has been minimized.
This comment has been minimized.
beshkenadze
commented
May 22, 2015
|
Temporary solution: downgrade OkHttp to version 2.3.0. |
This comment has been minimized.
This comment has been minimized.
crossle
commented
May 23, 2015
|
I used OkHttp 2.3.0, report this error |
This comment has been minimized.
This comment has been minimized.
beshkenadze
commented
May 23, 2015
|
@crossle Maybe the dependencies was getting a newer version of the library. |
This comment has been minimized.
This comment has been minimized.
|
As a reference: square/okhttp#751 |
This comment has been minimized.
This comment has been minimized.
|
This is fixed on |
JakeWharton
closed this
Jun 4, 2015
This comment has been minimized.
This comment has been minimized.
myanimal
commented
Jun 8, 2015
|
@JakeWharton any idea when we'll see this fix in a release? |
This comment has been minimized.
This comment has been minimized.
Defuera
commented
Jun 9, 2015
This comment has been minimized.
This comment has been minimized.
beshkenadze
commented
Jun 9, 2015
|
@Defuera This is not a solution, it use POST variables instead of GET variables. |
This comment has been minimized.
This comment has been minimized.
wahidm
commented
Jun 12, 2015
|
Do you have an update on when a release will be available with this fix? |
This comment has been minimized.
This comment has been minimized.
|
No. You can add an empty body yourself with |
This comment has been minimized.
This comment has been minimized.
viniciuscb
commented
Jun 12, 2015
|
same problem for me, it was working yesterday, not today anymore. |
This comment has been minimized.
This comment has been minimized.
antocara
commented
Jun 19, 2015
|
compile 'com.squareup.retrofit:retrofit:1.9.0' Today same problem, but the solution of Jake all ok, "@Body String body" |
This comment has been minimized.
This comment has been minimized.
beshkenadze
commented
Jun 23, 2015
|
@anpstudio try downgrade to 2.3.0. |
This comment has been minimized.
This comment has been minimized.
IgorGanapolsky
commented
Jul 2, 2015
|
I am having the same problem since upgrading to Retrofit 1.9.0 (with OkHttp 2.4.0) |
This comment has been minimized.
This comment has been minimized.
ChocolateMinht
commented
Aug 21, 2015
|
Had the same issue with Retrofit 1.9.0 + OkHttp 2.4.0. Downgrading to OkHttp 2.3.0 temporarily resolves the issue. |
This comment has been minimized.
This comment has been minimized.
WOSHICAIXIANFENG
commented
Aug 24, 2015
|
compile 'com.squareup.okhttp:okhttp:2.3.0' |
This comment has been minimized.
This comment has been minimized.
martin-gearzero
commented
Sep 4, 2015
|
This is still an issue in 2.5.0:
Curiously the OkHttp changelog for v2.4.0 says: • "Fix: OkApacheClient now allows an empty PUT and POST." This seems to be the version in which the error started since v2.3.0 works fine. |
This comment has been minimized.
This comment has been minimized.
|
The workaround is to use an empty body, but using The solution was to use a class EmptyOutput implements TypedOutput {
public static final TypedOutput INSTANCE = new EmptyOutput();
private EmptyOutput() { }
@Override
public String fileName() {
return null;
}
@Override
public String mimeType() {
return "application/json";
}
@Override
public long length() {
return 0;
}
@Override
public void writeTo(OutputStream out) throws IOException {
}
}Then I can use it in the method signature: @POST("/some/url)
Observable<Thing> doThing(@Body TypedOutput empty);Then I call it using my instance:
|
This comment has been minimized.
This comment has been minimized.
abhishek-ucreate
commented
Oct 21, 2015
|
downgrading to okhttp2.3 worked for me |
This comment has been minimized.
This comment has been minimized.
waqary705
commented
Nov 9, 2015
This comment has been minimized.
This comment has been minimized.
WonderCsabo
commented
Nov 25, 2015
|
By extending #854 (comment) solution, you can create the following client: package your.package;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.internal.http.HttpMethod;
import java.io.IOException;
import java.io.OutputStream;
import retrofit.client.OkClient;
import retrofit.client.Request;
import retrofit.client.Response;
import retrofit.mime.TypedOutput;
/**
* Workaround for https://github.com/square/retrofit/issues/854 .
*/
public class NullBodyAwareOkClient extends OkClient {
public NullBodyAwareOkClient() { }
public NullBodyAwareOkClient(OkHttpClient okHttpClient) {
super(okHttpClient);
}
@Override
public Response execute(Request request) throws IOException {
if (HttpMethod.requiresRequestBody(request.getMethod()) && request.getBody() == null) {
Request newRequest = new Request(request.getMethod(), request.getUrl(), request.getHeaders(), EmptyOutput.INSTANCE);
return super.execute(newRequest);
}
return super.execute(request);
}
private static class EmptyOutput implements TypedOutput {
static final TypedOutput INSTANCE = new EmptyOutput();
private EmptyOutput() { }
@Override
public String fileName() {
return null;
}
@Override
public String mimeType() {
return "application/json";
}
@Override
public long length() {
return 0;
}
@Override
public void writeTo(OutputStream out) throws IOException {
}
}
}Then use it in your new RestAdapter.Builder()
.setClient(new NullBodyAwareOkClient())
// ...This way you can get rid of the dummy |
This comment has been minimized.
This comment has been minimized.
yoganlegendkiller
commented
Dec 8, 2015
|
Am using retrofit 1.9.0 along with okhttp 2.4.0 is there any default json converter for retrofit 1.9 am making async call in interface but when in try to implement it throws service method cannot return void don't know where am doing mistake |
niftylettuce
referenced this issue
Dec 20, 2015
Closed
Android - Method POST must have a request body #4890
This comment has been minimized.
This comment has been minimized.
vovkab
commented
Jan 8, 2016
|
Still reproduceable in 2.7.2:
|
This comment has been minimized.
This comment has been minimized.
calren
commented
Jan 14, 2016
|
also seeing this issue in 2.7.2 |
pgreze
referenced this issue
Jan 15, 2016
Closed
Issue with OkHttp > 2.3.0 (method POST must have a request body) #32
This comment has been minimized.
This comment has been minimized.
pgreze
commented
Jan 15, 2016
|
I don't understand: this bug is very old, seems closed but still in the latest version. This problem has broken many libraries (like twitter-core) and many developers are probably stuck with an old version. Could you reopen the issue or explain this breaking change in public API? (Thanks for your answer and this wonderful library |
This comment has been minimized.
This comment has been minimized.
WonderCsabo
commented
Jan 15, 2016
This comment has been minimized.
This comment has been minimized.
pgreze
commented
Jan 15, 2016
|
@WonderCsabo Nice! But what about 1.x releases? |
This comment has been minimized.
This comment has been minimized.
WonderCsabo
commented
Jan 15, 2016
|
Well that fix was added before changing the version to 2.x, so it is possible, but only @JakeWharton and @swankjesse can know they want to release an update for 1.x or not . :) |
This comment has been minimized.
This comment has been minimized.
efrohnhoefer
commented
Feb 4, 2016
pushed a commit
to wikimedia/apps-android-wikipedia
that referenced
this issue
Feb 19, 2016
This comment has been minimized.
This comment has been minimized.
xiaolongyuan
commented
Apr 29, 2016
|
+1 |
This comment has been minimized.
This comment has been minimized.
pixoo
commented
Jul 7, 2016
|
I've just sent empty JSON instead of empty String
and then I just called my method like this:
Hope it will help if some of users have similar problem. |
This comment has been minimized.
This comment has been minimized.
aitbaali
commented
Aug 14, 2016
•
|
|
This comment has been minimized.
This comment has been minimized.
Isanderthul
commented
Feb 26, 2017
|
@pixoo I did as you suggested, thank you, it was the easiest solution. Just a note that on using
and then |
crossle commentedMay 20, 2015
Why?