如何从请求正文中需要字段的 api 端点获取 Retrofit 调用的 HEAD?
How to get a HEAD of a Retrofit call from an api endpoint that requires field in the request body?
I am trying to examine the headers of a response from an API call made via Retrofit 2.0.2 before actually downloading the content.
My API interface looks like the following:
@Headers({"Accept: application/json", "Origin: http://www.example.com"})
@HEAD("profiles")
Call<Void> getProfileHeaders(@Field("puids") String puid);
Please note that the API call requires me to specify in the body a field called puids=%{UUID}
list of UUIDs in order to return a response.
If I would like to download the data without examining the headers first, I would just call an interface like this:
@Headers({"Accept: application/json", "Origin: http://www.example.com"})
@FormUrlEncoded
@POST("profiles")
Call<String> getProfile(@Field("puids") String puid);
Now the issue is that when I try to use the getProfileHeader()
endpoint, I get the following RuntimeException:
java.lang.IllegalArgumentException: @Field parameters can only be used with form encoding. (parameter #1)
In order to use the @Field parameters (as I suppose a POST method would normally would do if required), I would have to explicitly specify that I use @FormUrlEncoded
, but I can't make a @HEAD
call with that.
I am a bit puzzled how could I achieve what I want and what am I missing?
Basically I would like to know how can I examine a retrofit call's response headers before downloading the actual body, of an API endpoint that requires field parameters?
干杯!
改造使用的 Okhttp 具有拦截器,可让您即时修改或检查请求。查看 github documentation
好的,我刚刚意识到我的困惑源于几个误解:
@HEAD
是一种 HTTP 方法,通常用于验证超链接的有效性和服务器对 GET
调用的响应。它不适用于 POST
请求,理论上是不正确的。
取自 HTTP/1.1 定义的 RFC2616
:
The HEAD method is identical to GET except that the server MUST NOT
return a message-body in the response. The metainformation contained
in the HTTP headers in response to a HEAD request SHOULD be identical
to the information sent in response to a GET request. This method can
be used for obtaining metainformation about the entity implied by the
request without transferring the entity-body itself. This method is
often used for testing hypertext links for validity, accessibility,
and recent modification.
The response to a HEAD request MAY be cacheable in the sense that the
information contained in the response MAY be used to update a
previously cached entity from that resource. If the new field values
indicate that the cached entity differs from the current entity (as
would be indicated by a change in Content-Length, Content-MD5, ETag or
Last-Modified), then the cache MUST treat the cache entry as stale.
- 根据定义发出
POST
请求时,我们已经计算了响应 server-side 并花时间下载了考虑中的 body。
POST
方法的其中一个函数,如 RFC2616
中所定义:
- Providing a block of data, such as the result of submitting a form, to a data-handling process;
因此为了不下载 body 而验证 header 超出了这个目的。
正如上面@Radek 所提到的,在 GET
请求上使用拦截器来修改 and/or 即时检查请求可以完成这项工作,但那时我们也可以启动 HEAD
方法请求。
这个问题的解决方案是通过对 server-side 进行更改而不是 returning 来更好地与 RFC2616
中定义的标准定义保持一致原始数据块作为 POST 响应,使其成为 return 资源,然后在 GET
/HEAD
请求中调用。所有只是重构服务调用以使用 GET
而不是 POST
.
I am trying to examine the headers of a response from an API call made via Retrofit 2.0.2 before actually downloading the content.
My API interface looks like the following:
@Headers({"Accept: application/json", "Origin: http://www.example.com"})
@HEAD("profiles")
Call<Void> getProfileHeaders(@Field("puids") String puid);
Please note that the API call requires me to specify in the body a field called puids=%{UUID}
list of UUIDs in order to return a response.
If I would like to download the data without examining the headers first, I would just call an interface like this:
@Headers({"Accept: application/json", "Origin: http://www.example.com"})
@FormUrlEncoded
@POST("profiles")
Call<String> getProfile(@Field("puids") String puid);
Now the issue is that when I try to use the getProfileHeader()
endpoint, I get the following RuntimeException:
java.lang.IllegalArgumentException: @Field parameters can only be used with form encoding. (parameter #1)
In order to use the @Field parameters (as I suppose a POST method would normally would do if required), I would have to explicitly specify that I use @FormUrlEncoded
, but I can't make a @HEAD
call with that.
I am a bit puzzled how could I achieve what I want and what am I missing?
Basically I would like to know how can I examine a retrofit call's response headers before downloading the actual body, of an API endpoint that requires field parameters?
干杯!
改造使用的 Okhttp 具有拦截器,可让您即时修改或检查请求。查看 github documentation
好的,我刚刚意识到我的困惑源于几个误解:
@HEAD
是一种 HTTP 方法,通常用于验证超链接的有效性和服务器对GET
调用的响应。它不适用于POST
请求,理论上是不正确的。
取自 HTTP/1.1 定义的 RFC2616
:
The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.
The response to a HEAD request MAY be cacheable in the sense that the information contained in the response MAY be used to update a previously cached entity from that resource. If the new field values indicate that the cached entity differs from the current entity (as would be indicated by a change in Content-Length, Content-MD5, ETag or Last-Modified), then the cache MUST treat the cache entry as stale.
- 根据定义发出
POST
请求时,我们已经计算了响应 server-side 并花时间下载了考虑中的 body。
POST
方法的其中一个函数,如 RFC2616
中所定义:
- Providing a block of data, such as the result of submitting a form, to a data-handling process;
因此为了不下载 body 而验证 header 超出了这个目的。
正如上面@Radek 所提到的,在 GET
请求上使用拦截器来修改 and/or 即时检查请求可以完成这项工作,但那时我们也可以启动 HEAD
方法请求。
这个问题的解决方案是通过对 server-side 进行更改而不是 returning 来更好地与 RFC2616
中定义的标准定义保持一致原始数据块作为 POST 响应,使其成为 return 资源,然后在 GET
/HEAD
请求中调用。所有只是重构服务调用以使用 GET
而不是 POST
.