[英]Retrofit request Header with params
我想在Android應用程序中使用兩個標頭進行請求,第一個標頭具有一個參數。 我嘗試了這個:
@Headers("Content-Type: application/json")
@GET("/ebuzon/client/show")
Call<Client> showClient(@Header("Authorization") String token);
call = api.showClient(" Bearer " +MySharedUser.getInstance().getToken());
401虛無
編輯:這應該工作,請參閱@Robert答案。 我的問題也在更新令牌
@GET("/ebuzon/client/show")
Call<Client> showClient(@Header("Authorization") String token,
@Header("Content-Type") String appJson);
call = api.showClient(" Bearer" +MySharedUser.getInstance().getToken(), "application/json");
401虛無
@Headers({"Authorization: Bearer {token}","Content-Type: application/json"})
@GET("/ebuzon/client/show")
Call<Client> showClient(@Path("token") String token);
IllegalArgumentException:URL“ / ebuzon / client / show”不包含“ {token}”。 (參數1)
我在這樣的角度做同樣的事情,它正在工作
var obj = this.http.get("/ebuzon/client/show",
{headers: new HttpHeaders().set("Authorization", " Bearer "+ this.token).append("Content-Type","application/json")})
.map(res => {
console.log(res);
return res
})
.catch(err => {
console.log(err);
return err;
})
return obj;
謝謝你的幫助。
也許這會幫到您。.也許您需要在請求中添加標題。
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request().newBuilder()
.addHeader("Authorization", "Bearer " + token)
.build();
return chain.proceed(newRequest);
}
}).build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.baseUrl(/** your url **/)
.addConverterFactory(GsonConverterFactory.create())
.build();
方法1或2應該起作用。
我相信問題可能出在您構建價值的方式上。
(" Bearer" +MySharedUser.getInstance().getToken());
這將導致類似
" Bearer{SOME_TOKEN}"
實際上,您最有可能希望它像
"Bearer {SOME_TOKEN}"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.