@Headers({ "Connection: keep-alive", "Access-Control-Request-Method: GET", "Origin: https://unsplash.com", "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.83 Safari/537.36", "Access-Control-Request-Headers: authorization,stream-auth-type,x-stream-client", "Accept: */*", "Referer: https://unsplash.com/", "Accept-Encoding: gzip, deflate, sdch, br", "Accept-Language: zh-CN,zh;q=0.8,en;q=0.6" }) @OPTIONS("api/v1.0/feed/notification/{numeric_id}/") Call<ResponseBody> optionFirstPageStream(@Path("numeric_id") int numeric_id, @Query("limit") int limit, @Query("api_key") String api_key, @Query("location") String location);
private void parseMethodAnnotation(Annotation annotation) { if (annotation instanceof DELETE) { parseHttpMethodAndPath("DELETE", ((DELETE) annotation).value(), false); } else if (annotation instanceof GET) { parseHttpMethodAndPath("GET", ((GET) annotation).value(), false); } else if (annotation instanceof HEAD) { parseHttpMethodAndPath("HEAD", ((HEAD) annotation).value(), false); if (!Void.class.equals(responseType)) { throw methodError("HEAD method must use Void as response type."); } } else if (annotation instanceof PATCH) { parseHttpMethodAndPath("PATCH", ((PATCH) annotation).value(), true); } else if (annotation instanceof POST) { parseHttpMethodAndPath("POST", ((POST) annotation).value(), true); } else if (annotation instanceof PUT) { parseHttpMethodAndPath("PUT", ((PUT) annotation).value(), true); } else if (annotation instanceof OPTIONS) { parseHttpMethodAndPath("OPTIONS", ((OPTIONS) annotation).value(), false); } else if (annotation instanceof HTTP) { HTTP http = (HTTP) annotation; parseHttpMethodAndPath(http.method(), http.path(), http.hasBody()); } else if (annotation instanceof retrofit2.http.Headers) { String[] headersToParse = ((retrofit2.http.Headers) annotation).value(); if (headersToParse.length == 0) { throw methodError("@Headers annotation is empty."); } headers = parseHeaders(headersToParse); } else if (annotation instanceof Multipart) { if (isFormEncoded) { throw methodError("Only one encoding annotation is allowed."); } isMultipart = true; } else if (annotation instanceof FormUrlEncoded) { if (isMultipart) { throw methodError("Only one encoding annotation is allowed."); } isFormEncoded = true; } }
@Test public void options() { class Example { @OPTIONS("/foo/bar/") // Call<ResponseBody> method() { return null; } } Request request = buildRequest(Example.class); assertThat(request.method()).isEqualTo("OPTIONS"); assertThat(request.headers().size()).isZero(); assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/"); assertThat(request.body()).isNull(); }
@Headers({ "Connection: keep-alive", "Access-Control-Request-Method: GET", "Origin: https://unsplash.com", "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.83 Safari/537.36", "Access-Control-Request-Headers: authorization,stream-auth-type,x-stream-client", "Accept: */*", "Referer: https://unsplash.com/", "Accept-Encoding: gzip, deflate, sdch, br", "Accept-Language: zh-CN,zh;q=0.8,en;q=0.6" }) @OPTIONS Call<ResponseBody> optionNextPageStream(@Url String next_page);
@OPTIONS() Observable<ResponseBody> options(@Url String url, @QueryMap Map<String, String> maps);
/** * 发起OPTIONS类型请求 * * @param url * @param headerMap * @param paramMap * @return */ @OPTIONS Call<ResponseBody> executeOptions( @Url String url, @HeaderMap Map<String, String> headerMap, @QueryMap Map<String, String> paramMap);