Search code examples
androidheaderretrofitbasic-authentication

How to send post request with basic auth in retrofit?


In my code, I want to send post request with basic auth.

Here is my postman screenshot :

enter image description here

here is my apiInterface class

@FormUrlEncoded
    @POST("GetBarcodeDetail")
    Call<PreliminaryGoodsAcceptResponse> PRELIMINARY_GOODS_ACCEPT_RESPONSE_CALL(@Field("ProcName") String procName, @Field("Barcode") String barcode, @Field("LangCode") String langCode);

here is my apiclient

public class ApiClient {

    public static final String BASE_URL = "http://192.**********";
    private static Retrofit retrofit = null;
    private static OkHttpClient sClient;

    public static Retrofit getClient() {
        if(sClient == null) {
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            sClient = new OkHttpClient.Builder()
                    .addInterceptor(new HttpLoggingInterceptor(HttpLoggingInterceptor.Logger.DEFAULT))
                    .addInterceptor(interceptor)
                    .build();
        }

        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(sClient)
                    .build();
        }
        return retrofit;
    }

}

My question is how can i send post request,using header :

Header Username : EBA Token :
34242353453456563DSFS


Solution

  • make header like this way..

     private Retrofit getClient(final Context context) {
    
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder client = new OkHttpClient.Builder();
        client.readTimeout(60, TimeUnit.SECONDS);
        client.writeTimeout(60, TimeUnit.SECONDS);
        client.connectTimeout(60, TimeUnit.SECONDS);
        client.addInterceptor(interceptor);
        client.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                if (context == null) {
                    request = request
                            .newBuilder()
                            .build();
                } else {
                    request = request
                            .newBuilder()
                            .addHeader("Authorization", "Bearer " + AppSetting.getStringSharedPref(context, Constants.USER_KEY_TOKEN, ""))
                            .build();
                }
                return chain.proceed(request);
            }
        });
    
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(client.build())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    
    
        return retrofit;
    }