ooCHURIoo
ooCHURIoo

Reputation: 191

retrofit 2 @headers error in kotlin

I try to add multiple static headers with retrofit 2 (2.3.0) like this :

interface WeatherAPI {

    @Headers({
        "Accept: application/json",
        "Content-type:application/json"
    })
    @GET("/data/2.5/weather")
    fun getWeatherForCityName(@Query("q") city: String, @Query("appid") appid: String) : Call<GetWeatherResponse>;

}

I have the folowing error :

error

Any idea of my mistake?

Upvotes: 16

Views: 16444

Answers (3)

Phu
Phu

Reputation: 123

You should use:

@Headers(value = ["Accept: application/json",
          "Content-type:application/json"])

because the headers need an array parameter for it, you are using the wrong syntax in kotlin

Upvotes: 8

user4571931
user4571931

Reputation:

Use below code:

@Headers(
        "Accept: application/json",
        "Content-type:application/json"
)

Upvotes: 23

osipxd
osipxd

Reputation: 1228

Braces {} isn't needed inside @Headers.

Docs: https://kotlinlang.org/docs/reference/annotations.html#arrays-as-annotation-parameters

Upvotes: 45

Related Questions