title: Dynamically pass header and body in retrofit
body: <p>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:</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>following is my Interface:</p>
  
  <pre><code>public interface RegisterInterface {
    @POST("api/userData/registerNewUserData")
  Call&lt;RegisterModel&gt; getData(@Body RequestBody body);
  </code></pre>
  
  <p>}</p>
  
  <p>And following i the class where i need to use the api:</p>
  
  <pre><code>registerInterface = ApiClient.getApiClient(accessToken).create(RegisterInterface.class);
                  Call&lt;RegisterModel&gt; call = registerInterface.getData(requestBody);
                  call.enqueue(new Callback&lt;RegisterModel&gt;() {
                      @Override
                      public void onResponse(Call&lt;RegisterModel&gt; call, Response&lt;RegisterModel&gt; 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&lt;RegisterModel&gt; call, Throwable t) {
  
                      }
                  });
              }
              else {
                  Toast.makeText(RegisterClass.this, "Please fill all the fields", Toast.LENGTH_SHORT).show();
              }
          }
      });
  </code></pre>
  
  <p>the status code is 401. I dont know what wrong did i do. Cna somebody please help me?</p>