Ask Your Question
4

Is there a way to add Headers to a Retrofit API request that does not involve utilizing an Interceptor?

asked 2022-02-15 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-03-16 08:00:00 +0000

devzero gravatar image

Yes, there is a way to add headers to a Retrofit API request without using an Interceptor. You can add headers directly to the method or the request using the @Headers annotation in Retrofit.

For example:

@Headers("Authorization: token123")
@GET("user")
Call<User> getUser();

This will add an "Authorization" header with the value "token123" to the GET request for the "user" endpoint.

You can also add multiple headers using the @Headers annotation with multiple values:

@Headers({
    "Authorization: token123",
    "Accept: application/json"
})
@GET("user")
Call<User> getUser();

This will add both "Authorization" and "Accept" headers to the GET request.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-02-15 11:00:00 +0000

Seen: 22 times

Last updated: Mar 16 '22