Retrofit: how to use okhttp request tag in retrofit

Created on 7 Jul 2017  Â·  13Comments  Â·  Source: square/retrofit

I used okhttp  request with tag for my internet request, Now i decide to use retrofit

But i find there isn't any API in retrofit for me to add a tag which is support by okhttp to request;
The request tag is important for me ,and retrofit is convenient for us,So I want to combine them
Can you help to add this function in retrofit or tell how i can build retrofit source code to satisfy my need

thank you

Most helpful comment

class Client {
@GET("/url")
@Tag("Authorized") // custom tag that will trigger a Interceptor logic
String somemethod();

i'd like to have a fine grained control over OkHttp Interceptors

class AuthInterceptor implements Interceptor {
Response intercept {
Request req = request()
if (req.tag == "...") { dosomething() }
}

All 13 comments

There's no Retrofit 2 API directly exposed to set the Request tag.

You could make a CallAdapter to do this, though.

I think i should change RequestBuilder source code to add tag

What are you using it for?

On Mon, Jul 10, 2017, 1:19 AM xushaun notifications@github.com wrote:

I think i should change RequestBuilder source code to add tag

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/square/retrofit/issues/2394#issuecomment-314008248,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAEEET5VIP2A8iQnipppb5BNIg6AEa3_ks5sMbRXgaJpZM4OQdDV
.

I used tag to Distinguish between different requst, Before i send http request to server, i will attach extra information to this request with okhttp tag, so I can Distinguish between different requst and response

@xushaun you can try get request by Interceptor

I have a similar requirement of distinguishing requests with tags, but I wouldn't want to use call adapter factory as I'm using rxJava2Call factory already. I could do it in the Call.Factory per se but annotations could make it simpler, otherwise I'd have to map tags to urls.

I also came across with the same requirement. I somehow need to identify what request is running on the interceptor so that I can modify some headers accordingly.

Why can not add the request tag in Retrofit, the purpose of this design is

until now there was no support?

class Client {
@GET("/url")
@Tag("Authorized") // custom tag that will trigger a Interceptor logic
String somemethod();

i'd like to have a fine grained control over OkHttp Interceptors

class AuthInterceptor implements Interceptor {
Response intercept {
Request req = request()
if (req.tag == "...") { dosomething() }
}

Great idea thx

I used okhttp  request with tag for my internet request, Now i decide to use retrofit 

But i find there isn't any API in retrofit for me to add a tag which is support by okhttp to request;
The request tag is important for me ,and retrofit is convenient for us,So I want to combine them
Can you help to add this function in retrofit or tell how i can build retrofit source code to satisfy my need

thank you

you can use the retrofit @Tag annotation in the request method.

eg: Call> userList(@Path("user") String user, @Tag() String value);

Was this page helpful?
0 / 5 - 0 ratings