Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 779
cartoonworld Avatar asked May 24 '18 12:05

cartoonworld


People also ask

How do I send a request with basic authentication?

Basic authentication involves sending a verified username and password with your request. In the request Authorization tab, select Basic Auth from the Type dropdown list. Enter your API username and password in the Username and Password fields. For additional security, store these in variables.

How do you use basic authentication in retrofit?

Locate the build. gradle (Module:app) file and add your latest retrofit dependencies. OkHttp interceptors are powerful as they can monitor, rewrite, retry calls. The interceptor here will be used to inject Basic Authentication to every request to the web service.


2 Answers

This is so far the easiest method i have ever tried for "Basic Authentication".

Use the below code to generate the auth header (API/Repository class)

 var basic = Credentials.basic("YOUR_USERNAME", "YOUR_PASSWORD")

Pass this as header to the webservice call (API/Repository class)

 var retrofitCall = myWebservice.getNewsFeed(basic)

Add the basic header as parameter (Retrofit Webservice interface class)

 @GET("newsfeed/daily")
 fun getNewsFeed(@Header("Authorization") h1:String):Call<NewsFeedResponse>

Sorry, my code is in Kotlin, but can be easily translated to Java.

References: https://mobikul.com/basic-authentication-retrofit-android/

like image 64
Ajith Memana Avatar answered Oct 09 '22 18:10

Ajith Memana


Use Header annotation

@FormUrlEncoded
    @POST("GetBarcodeDetail")
    Call<PreliminaryGoodsAcceptResponse> PRELIMINARY_GOODS_ACCEPT_RESPONSE_CALL(@Header("Authorization") token: String,@Field("ProcName") String procName, @Field("Barcode") String barcode, @Field("LangCode") String langCode);
like image 28
Mirza Ahmed Baig Avatar answered Oct 09 '22 18:10

Mirza Ahmed Baig