【问题标题】:Retrofit is stripping trailing slash in urlRetrofit 正在去除 url 中的斜杠
【发布时间】:2019-10-26 02:11:28
【问题描述】:

我有一个这样的网址:

https://www.dummy.com/api/v1/

public interface DummyService {
    @POST("path/")
    Observable<Response<Void>> postToDummy(@Body String someBody);
}

我想将 / 保留在我的 url 末尾(路径之后),但是当改造发出 REST 请求时,它会删除 /。我怎样才能防止这种情况发生?

这是 Retrofit 向以下地址发出请求的 URL:
https://www.dummy.com/api/v1/path

这就是我想要的:
https://www.dummy.com/api/v1/path/

【问题讨论】:

  • 您如何验证 Retrofit 是否正在从您的路径中删除尾随 /?根据Retrofit.Builder.baseUrl()的文档,应该不是:square.github.io/retrofit/2.x/retrofit/retrofit2/…
  • @BenP。我正在使用 httplogginginterceptor 将请求打印到控制台进行改造,它显示请求缺少尾随 /。通过使用和不使用/ 在邮递员上进行验证,我也得到了相同的响应。

标签: android rest retrofit retrofit2


【解决方案1】:

我认为您不需要末尾的斜杠,因为它不会更改目标 URL。如果您在向端点添加路径时遇到问题,可以使用 Retrofit 提供的 @Path 注释。

【讨论】:

  • 对我来说确实如此。请求在没有尾随 / 的情况下失败并成功。
猜你喜欢
  • 2015-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多