Search code examples
androidretrofit2rapidapi

How do i add headers when using retrofit


So this is my first time working with API's and am trying to use a gym exercises api from rapid api . The site has offered to give me a code snippet in these forms (OKHttp, Unirest, AsyncHttp, java.net.http), but i still have no clue how how do i implement it. I have a bit of knowledge about retrofit, but i have no clue how do i implement any of the snippet codes using retrofit or if that's even necessary. Any guidance regarding my problem will be much appreciated.


Solution

  • so i have figured id out. the problem was how do i add the headers given by rapidApi in my retrofit object(I didn't even know what headers were when i asked this question), Which is quite easy. all i had to do is add an @Headers annotation on top of the used method with the headers key and value, Like this :

    @Headers({"randomKey1: 45df3fsd5432gsdg43s244dsg5643",
            "randomKey2: exercisedb.p.rapidapi.com"})
    @GET("pectorals")
    Call<List<TargetMuscle>> getExercises();
    

    and then continue to use retrofit as i would usually do.