Search code examples
androidretrofit

Retrofit2 GET request with Id


I am trying to make a request in my android project. The url is this one

"https://api.spoonacular.com/recipes/716429/information?includeNutrition=false"

I am using retrofit 2 but i can't figure out how to make it work.

Here is what i tried to do.

I get the id of a recipe and call this function passing the id.

 fun applyQueryById(recipeId: Int): String{
   val  searchByIdQuery = "${recipeId}/information?includeNutrition=false&apiKey=${API_KEY}"
    return searchByIdQuery
}

And the GET request is this one

   @GET("/recipes/")
suspend fun getRecipeById(
    @Query("id") searchById:String
):Response<PersonalizedRecipeResult>

I thinks that because the id is in the middle, making a raw string like i am doing is not a good idea. if anyone could suggest something different I'll appreciate


Solution

  • You are using @Query("id") which will ad the value as query.

    From your example, I can see that you want to use @Path

    You can use it like this

       @GET("/recipes/{id}")
       suspend fun getRecipeById(
          @Path("id") searchById:String
       ):Response<PersonalizedRecipeResult>
    

    by this way the searchById will be replaced with {id} in your example call