Skip to content
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

POST not work with @field annotaion #3967

Closed
1 of 3 tasks
calidion opened this issue Sep 21, 2023 · 10 comments
Closed
1 of 3 tasks

POST not work with @field annotaion #3967

calidion opened this issue Sep 21, 2023 · 10 comments

Comments

@calidion
Copy link

What kind of issue is this?

  • Question. This issue tracker is not the place for questions. If you want to ask how to do
    something, or to understand why something isn't working the way you expect it to, use Stack
    Overflow. https://stackoverflow.com/questions/tagged/retrofit

  • Bug report. If you’ve found a bug, spend the time to write a failing test. Bugs with tests
    get fixed. Here’s an example: https://gist.github.com/swankjesse/6608b4713ad80988cdc9

  • Feature Request. Start by telling us what problem you’re trying to solve. Often a solution
    already exists! Don’t send pull requests to implement new features without first getting our
    support. Sometimes we leave features out on purpose to keep the project small.

version 2.9.0

@JakeWharton
Copy link
Member

Here's a test case showcasing that it can work:

@Test
public void simpleFormEncoded() {
class Example {
@FormUrlEncoded //
@POST("/foo") //
Call<ResponseBody> method(@Field("foo") String foo, @Field("ping") String ping) {
return null;
}
}
Request request = buildRequest(Example.class, "bar", "pong");
RequestBody body = request.body();
assertBody(body, "foo=bar&ping=pong");
assertThat(body.contentType().toString()).isEqualTo("application/x-www-form-urlencoded");
}

Please supply a failing test case if you think something is broken.

@calidion
Copy link
Author

I think this simple test may be not sufficient.
I have seem a lot of question on why field is not working.
no one can give a working answer.
I am now using okhttpclient instead.

@calidion
Copy link
Author

this is the code working with OkHttpClient:

        public String quota(String username , String password) {
        RequestBody formBody = new FormBody.Builder()
                .add("username", username)
                .add("password", password)
                .build();

        Request request = new Request.Builder()
                .url(BASE_URL + "user/quota")
                .post(formBody)
                .build();

        Call call = httpClient.newCall(request);
            try {
                Response response = call.execute();
                return response.body().string();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

@calidion
Copy link
Author

this is the code not working with retrofit2:



    public Quota quota;
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

    public void start(String username, String password) {
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .client(httpClient.build())
                .build();

        APIService apiService = retrofit.create(APIService.class);

        Call<Quota> quotaCall = apiService.getQuota(username, password);
        quotaCall.enqueue(this);
    }

    @Override
    public void onResponse(Call<Quota> call, Response<Quota> response) {
        if (response.isSuccessful()) {
            quota = response.body();
        } else {
            System.out.println(response.errorBody());
        }
    }

    @Override
    public void onFailure(Call<Quota> call, Throwable t) {
        t.printStackTrace();
    }
public interface Quota {
    public String username = "";
    public int quota = 0;
    public int upload = 0;
    public int download = 0;
}
public interface APIService {
    @GET("servers")
    void getServers();

    @FormUrlEncoded
    @POST("user/quota")
    Call<Quota> getQuota(@Field("username") String username, @Field("password") String password);

}

@JakeWharton
Copy link
Member

How is it not working?

@calidion
Copy link
Author

How is it not working?

I can't trace much deeper.
I only know that the parameters are passed into the requester.

@calidion
Copy link
Author

calidion commented Sep 22, 2023

@JakeWharton

https://github.com/Free-Web-Movement/android-base-library

this is a repository can reproduce this bug.
Where OKHTTP works well and @field not .

@JakeWharton
Copy link
Member

Retrofit's @FormUrlEncoded uses OkHttp's FormBody.Builder to build the body the same as your snippet does.

if (isFormEncoded) {
// Will be set to 'body' in 'build'.
formBuilder = new FormBody.Builder();

I need a self-contained test case to debug anything here, so I'm going to preemptively close. If you can come up with a failing test case I can take a look.

@JakeWharton JakeWharton closed this as not planned Won't fix, can't repro, duplicate, stale Mar 18, 2024
@calidion
Copy link
Author

this is the failing test case and you don't take a look.
And there is only one test!

@calidion
Copy link
Author

https://github.com/Free-Web-Movement/android-base-library
is the simplest example that showing how @field fails.
if you don't have the ability to debug, then you'd better resign or close source this project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants