改造 2 - POST 请求变成了 GET? [英] Retrofit 2 - POST request became GET?
问题描述
我的 POST
请求不断发送为 GET
&被 API 端点拒绝
My POST
request keeps sending as GET
& got rejected by API endpoint
MyService 类
@FormUrlEncoded
@POST("api/users/")
Call<List<User>> getUsers(@FieldMap HashMap<String, String> parameters);
请求代码
Gson builder = new GsonBuilder().setLenient().create();
Retrofit client = new Retrofit.Builder()
.baseUrl(Constants.API_ENDPOINT_TEST_URL)
.addConverterFactory(GsonConverterFactory.create(builder))
.build();
mApiService = client.create(MyService.class);
Call<List<User>> call = mApiService.getUsers(mParameters);
call.enqueue(new Callback<List<User>>() {
@Override
public void onResponse(Call<List<User>> call, Response<List<User>> response) {
mResponse = response.body();
mResponseObserver.onFinish(mResponse);
}
@Override
public void onFailure(Call<List<User>> call, Throwable t) {
mResponseObserver.onFailure();
}
});
但是当它以 GET
请求形式到达服务器时,服务器拒绝了它!?用调试器检查后,我看到:
But server rejects it as it reached the server in GET
request form !? Having checked with debugger, I saw that:
rawResponse.request.method = GET
这是显示改造请求对象的监视窗口的屏幕截图:
Here is screenshot of watch window showing the retrofit's request object:
如您所见,请求方法是 GET
.但奇怪的部分是在 tag
中,它显示了一个带有 POST
方法的请求对象?
As you can see, the request method was GET
. But the weird part is in the tag
, it shows a request object with POST
method?
我错过了什么吗?
更新
我添加了日志拦截器 &这是日志:
I added logging interceptor & here is the log:
D/OkHttp: --> POST http://***/api/users/ http/1.1
D/OkHttp: Content-Type: application/x-www-form-urlencoded
D/OkHttp: Content-Length: 56
D/OkHttp: --> END POST
D/OkHttp: <-- 200 OK https://***/api/users/ (388ms)
D/OkHttp: Date: Thu, 01 Sep 2016 11:50:23 GMT
D/OkHttp: Server: Apache
D/OkHttp: X-Powered-By: PHP/5.4.34
D/OkHttp: Cache-Control: max-age=2592000
D/OkHttp: Expires: Sat, 01 Oct 2016 11:50:23 GMT
D/OkHttp: Vary: Accept-Encoding
D/OkHttp: Content-Type: application/json; charset=UTF-8
D/OkHttp: Set-Cookie: SESSION_DEFAULT=***; expires=Sun, 04-Sep-2016 11:50:24 GMT; path=/; HttpOnly
D/OkHttp: Set-Cookie: COOKIE[***]=***; path=/; httponly
D/OkHttp: Connection: close
D/OkHttp: <-- END HTTP
看起来请求是一个POST
.但是,服务器仍然以错误消息响应,说请求方法是 GET
looks like the request is a POST
. But, the server still responds with error message, saying that the request method is a GET
嗯,我会深入研究一下.
Hmm, I'll dig a little bit more into it.
推荐答案
其实问题在于两个因素的结合:
Actually, the issue is the combination of 2 factors:
- 发出了错误的请求协议(http 而不是 https)
- 服务器回复了一条关于错误协议的奇怪消息:不支持 GET".
无论如何,感谢@nshmura 的帮助.
ANyway, thanks @nshmura for your assistant.
这篇关于改造 2 - POST 请求变成了 GET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!