Skip to content Skip to sidebar Skip to footer

Retrofit With Okhttp Not Set Content-type With @formurlencoded

I'm trying to implement auth via x-www-form-urlencoded with Retrofit 2 on Android but faced a problem, that Header Content-Type not set with @FormUrlEncoded annotation, as well as

Solution 1:

By this Request

@FormUrlEncoded@POST("account/login")
Single<LoginResponse> login(@Field("memberId") String memberId,
                                        @Field("pin") String pin);

method @POST and @FormUrlEncoded automatic add

Content-Type: application/x-www-form-urlencoded in header you can check in log by

HttpLoggingInterceptorinterceptor=newHttpLoggingInterceptor();

            OkHttpClientokHttpClient=newOkHttpClient.Builder()
                    .addInterceptor(interceptor.setLevel(HttpLoggingInterceptor.Level.BODY))
                    .connectTimeout(2, TimeUnit.MINUTES)
                    .writeTimeout(2, TimeUnit.MINUTES)
                    .readTimeout(2, TimeUnit.MINUTES)
                    .build();

it print all log in verbose mode

Post a Comment for "Retrofit With Okhttp Not Set Content-type With @formurlencoded"