Sudeep
Sudeep

Reputation: 67

Dynamically pass header and body in retrofit

I am trying to send http header and body using retrofit but there is an error. Can somebody please help me? following is my code: i have an ApiClient as follows:

public class ApiClient {
public static Retrofit retrofit = null;
public static Retrofit getApiClient(final String token) {

    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.addInterceptor(new Interceptor() {
                                  @Override
                                  public Response intercept(Interceptor.Chain chain) throws IOException {
                                      Request original = chain.request();

                                      Request request = original.newBuilder()
                                              .header("Authorization", "Bearer "+token)

                                              .method(original.method(), original.body())
                                              .build();

                                      return chain.proceed(request);
                                  }
                              });
    OkHttpClient client = httpClient.build();






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

following is my Interface:

public interface RegisterInterface {
  @POST("api/userData/registerNewUserData")
Call<RegisterModel> getData(@Body RequestBody body);

}

And following i the class where i need to use the api:

registerInterface = ApiClient.getApiClient(accessToken).create(RegisterInterface.class);
                Call<RegisterModel> call = registerInterface.getData(requestBody);
                call.enqueue(new Callback<RegisterModel>() {
                    @Override
                    public void onResponse(Call<RegisterModel> call, Response<RegisterModel> response) {
                        Log.e("code", response.code() + "");

                        String message = response.body().getMessage();
                        if (message.equals("success")) {
                            Toast.makeText(RegisterClass.this, "Data Sent Successfully", Toast.LENGTH_SHORT).show();
                            finish();
                            startActivity(getIntent());
                        }
                    }

                    @Override
                    public void onFailure(Call<RegisterModel> call, Throwable t) {

                    }
                });
            }
            else {
                Toast.makeText(RegisterClass.this, "Please fill all the fields", Toast.LENGTH_SHORT).show();
            }
        }
    });

the status code is 401. I dont know what wrong did i do. Cna somebody please help me?

Upvotes: 2

Views: 6190

Answers (1)

Navin
Navin

Reputation: 295

Used like below, use header parameter that are only required to pass

Map<String, String> stringStringMap = new HashMap<>();
        stringStringMap.put("Authorization", "Bearer "+token));
        stringStringMap.put("Content-Type", "application/json");
        stringStringMap.put("Accept", "application/json");

@POST("api/userData/registerNewUserData")
Call<RegisterModel> getData(@HeaderMap Map<String, String> headers,@Body RequestBody body);

Upvotes: 3

Related Questions