-
Notifications
You must be signed in to change notification settings - Fork 7.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
method POST must have a request body. #854
Comments
Should the okhttp report error. https://github.com/square/okhttp/blob/master/CHANGELOG.md |
I am getting the same exception , but currently I have used a workaround as per this SO post |
👍 Temporary solution: downgrade OkHttp to version 2.3.0. |
I used OkHttp 2.3.0, report this error |
@crossle Maybe the dependencies was getting a newer version of the library. |
As a reference: square/okhttp#751 |
This is fixed on |
@JakeWharton any idea when we'll see this fix in a release? |
@Defuera This is not a solution, it use POST variables instead of GET variables. |
Do you have an update on when a release will be available with this fix? |
No. You can add an empty body yourself with |
same problem for me, it was working yesterday, not today anymore. |
compile 'com.squareup.retrofit:retrofit:1.9.0' Today same problem, but the solution of Jake all ok, "@Body String body" |
@anpstudio try downgrade to 2.3.0. |
I am having the same problem since upgrading to Retrofit 1.9.0 (with OkHttp 2.4.0) |
Had the same issue with Retrofit 1.9.0 + OkHttp 2.4.0. Downgrading to OkHttp 2.3.0 temporarily resolves the issue. |
compile 'com.squareup.okhttp:okhttp:2.3.0' |
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. |
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:
|
downgrading to okhttp2.3 worked for me |
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 |
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 |
Still reproduceable in 2.7.2:
|
also seeing this issue in 2.7.2 |
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 😄 ) |
@WonderCsabo Nice! But what about 1.x releases? |
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 . :) |
square/retrofit#854 (comment) Change-Id: I738c1d739e8bc871e4392b64050e7083ab72c40f
+1 |
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. |
|
@Pixoo I did as you suggested, thank you, it was the easiest solution. Just a note that on using
and then |
Still an issue in 2019. Hopefully, there's the NullBodyAwareOkClient workaround posted above that works perfectly. |
Why?
The text was updated successfully, but these errors were encountered: