OKHTTP 3:如何基于路径参数添加标头

发布于 2025-02-11 14:18:31 字数 1308 浏览 0 评论 0原文

在OKHTTP中,如何添加基于ServiceID的标头? 一种方法是使用URL段。但这是基于索引。如果ServiceID在路径中始终处于相同的索引怎么办? 有没有办法按名称获取路径参数?这是一厢情愿的想法,因为我在任何地方都找不到这种方式。

改装:

@POST("services/{serviceId}/resources/{resourceId}")
Call<Resource> addResourceVector(@Path("serviceId") Long serviceId,
                                 @Path("resourceId") String resourceId)

Okhttp拦截器:

public class AuthHeaderInterceptor implements Interceptor {

    private final TokenService tokenService;

    public AuthHeaderInterceptor(TokenService tokenService) {
        this.tokenService = tokenService;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request originalRequest = chain.request();

        Request requestWithAuthToken = originalRequest.newBuilder()
            .header(HttpHeaders.AUTHORIZATION, tokenService.getToken(originalRequest.getPathParam("serviceId")) //wishful thinking
            .build();
        return chain.proceed(requestWithAuthToken);
    }
}

奖金: getToken返回完整的future。不要考虑在创业公司获得令牌,而是整个过程中使用相同的东西。 有一个建议异步拦截器的提议,但没有捡起。 https://github.com/square.com/square/okhttp/sissues/3714

In Okhttp, how to add a header that is based on serviceId?
One way would be to use url segments. But that is based on index. What if the serviceId is always not at same index in the path?
Is there a way to get path parameter by name? It's a wishful thinking as I couldn't find such a way anywhere.

Retrofit:

@POST("services/{serviceId}/resources/{resourceId}")
Call<Resource> addResourceVector(@Path("serviceId") Long serviceId,
                                 @Path("resourceId") String resourceId)

Okhttp Interceptor:

public class AuthHeaderInterceptor implements Interceptor {

    private final TokenService tokenService;

    public AuthHeaderInterceptor(TokenService tokenService) {
        this.tokenService = tokenService;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request originalRequest = chain.request();

        Request requestWithAuthToken = originalRequest.newBuilder()
            .header(HttpHeaders.AUTHORIZATION, tokenService.getToken(originalRequest.getPathParam("serviceId")) //wishful thinking
            .build();
        return chain.proceed(requestWithAuthToken);
    }
}

Bonus:
getToken returns CompletableFuture. Don't think about getting token at the startup and using the same throughout.
There was a proposal for async interceptor but it wasn't picked up.
https://github.com/square/okhttp/issues/3714

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海蓝天 2025-02-18 14:18:31

您可以使用Invocation标签恢复函数调用参数。

https://square.github.io/retrofit/2。 x/reatrofit/retrofit2/invocation.html

 class InvocationLogger implements Interceptor {
   @Override public Response intercept(Chain chain) throws IOException {
     Request request = chain.request();
     Invocation invocation = request.tag(Invocation.class);
     if (invocation != null) {
       System.out.printf("%s.%s %s%n",
           invocation.method().getDeclaringClass().getSimpleName(),
           invocation.method().getName(), invocation.arguments());
     }
     return chain.proceed(request);
   }
 }

You can recover the function call arguments using the Invocation tag.

https://square.github.io/retrofit/2.x/retrofit/retrofit2/Invocation.html

 class InvocationLogger implements Interceptor {
   @Override public Response intercept(Chain chain) throws IOException {
     Request request = chain.request();
     Invocation invocation = request.tag(Invocation.class);
     if (invocation != null) {
       System.out.printf("%s.%s %s%n",
           invocation.method().getDeclaringClass().getSimpleName(),
           invocation.method().getName(), invocation.arguments());
     }
     return chain.proceed(request);
   }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文