Retrofit2 Static encoded form data?

I have a new Android project that I'm working on, and Retrofit2 works well for me. However, I have one URL that I need to click, on one of two lines, on top of the data I send.

Now it looks like this:

@FormUrlEncoded @POST("token") Call<AccessResponse> requestAccess(@Field("grant_type") String type, @Field("code") String authCode, @Field("client_id") String ApiKey); 

grant type is only one of two things, and I would like to distract it in static URLs, for example:

 @FormUrlEncoded @POST("token") @Field("grant_type","type1") Call<AccessResponse> requestAccess( @Field("code") String authCode, @Field("client_id") String ApiKey); @FormUrlEncoded @POST("token") @Field("grant_type","type2") Call<AccessResponse> refreshAccess( @Field("code") String authCode, @Field("client_id") String ApiKey); 

Is there any way to do this? my 2 days google-fu did not work for me and did not browse documents and API code. I just don't want to track the correct line in different places of my code.

+5
source share
2 answers

Turns off the answer: "You cannot now."

There is an open issue on Github for requesting a function

There is an alternative approach - a FieldMap object with an example method mentioned in this SO page , but this is not quite what I was looking for, and overkill for just one field.

0
source

Can a Retrofit RequestInterceptor be done? It can enter parameters into each request ... so from there you could write a method that enters the correct parameter depending on what you are trying to do ...

 RequestInterceptor requestInterceptor = new RequestInterceptor() { @Override public void intercept(RequestFacade request) { request.addQueryParam("grant_type", getGrantType()); } }; private String getGrantType() { // do your stuff and : return "type1"; // or "type2" } 
0
source

Source: https://habr.com/ru/post/1241849/


All Articles