使用 URL 编码正文和用户身份验证改造 Post 请求
Retrofit Post Request with URL encoded body and user authentication
我正在尝试使用带有键值(&
分隔)负载和简单用户身份验证的改造来创建请求,因为可以在以下 curl
命令中完成:
curl -X POST -d "grant_type=refresh_token&refresh_token=<refresh_token>" -u"<client_id>:<client_secret>" http://localhost:8000/o/token/
如何在改造中创建提供上述数据的请求?我需要接口方法和 RequestBody
的创建(如果使用的话)。
更新:
我使用了下面的代码
接口方法定义:
@POST("/o/token/")
Observable<ServerToken> refreshAccessToken(@Header("Authorization") String auth, @Body RequestBody tokens);
调用实现:
RequestBody body = new FormEncodingBuilder()
.add("grant_type", "refresh_token")
.add("refresh_token", serverToken.refresh_token)
.build();
String auth = Credentials.basic("<client_id>", "<client_secret>");
mTokenApi.refreshAccessToken(auth, body)
但是我在请求中得到一个 Content-Type: application/json
和一个空的请求正文。
我该如何解决这个问题?
正确的做法是:
接口方法定义:
@FormUrlEncoded
@POST("/o/token/")
Observable<ServerToken> refreshAccessToken(@Header("Authorization") String auth,
@Field("grant_type") String grantType,
@Field("refresh_token") String refreshToken);
调用实现:
String auth = Credentials.basic(clientID, clientSecret);
mTokenApi.refreshAccessToken(auth, "refresh_token", serverToken.refresh_token)
我正在尝试使用带有键值(&
分隔)负载和简单用户身份验证的改造来创建请求,因为可以在以下 curl
命令中完成:
curl -X POST -d "grant_type=refresh_token&refresh_token=<refresh_token>" -u"<client_id>:<client_secret>" http://localhost:8000/o/token/
如何在改造中创建提供上述数据的请求?我需要接口方法和 RequestBody
的创建(如果使用的话)。
更新:
我使用了下面的代码
接口方法定义:
@POST("/o/token/")
Observable<ServerToken> refreshAccessToken(@Header("Authorization") String auth, @Body RequestBody tokens);
调用实现:
RequestBody body = new FormEncodingBuilder()
.add("grant_type", "refresh_token")
.add("refresh_token", serverToken.refresh_token)
.build();
String auth = Credentials.basic("<client_id>", "<client_secret>");
mTokenApi.refreshAccessToken(auth, body)
但是我在请求中得到一个 Content-Type: application/json
和一个空的请求正文。
我该如何解决这个问题?
正确的做法是:
接口方法定义:
@FormUrlEncoded
@POST("/o/token/")
Observable<ServerToken> refreshAccessToken(@Header("Authorization") String auth,
@Field("grant_type") String grantType,
@Field("refresh_token") String refreshToken);
调用实现:
String auth = Credentials.basic(clientID, clientSecret);
mTokenApi.refreshAccessToken(auth, "refresh_token", serverToken.refresh_token)