Andrey Danilov
Andrey Danilov

Reputation: 6602

OPTIONS/HEAD REST API request with Okhttp3

I`m writing some Rest client on Android and I met a problem - I have no idea how to make HEAD and OPTIONS requests.

There are no problems with GET/POST/PUT/DELETE/PATCH requests in OkHttp3, basically they looks like:

        request = new Request.Builder()
                .url(url)
                .headers(headerBuilder.build())
                .post(bodyBuilder.build())
                .build();

And OkHttp3 doesnt provide additional methods like head() or option().

So how can I make HEAD and OPTIONS requests using OkHttp3?

Upvotes: 10

Views: 4022

Answers (2)

mike.tihonchik
mike.tihonchik

Reputation: 6933

It appears (at least in the current implementation, API 3.12.0), HEAD request can be made just like GET and others:

Request request = new Request.Builder()
                .url(url)
                .head()
                .build();

OPTION still has to be implemented using .method()

Upvotes: 9

Andrey Danilov
Andrey Danilov

Reputation: 6602

Found answer, may be it will be useful for someone else

OkHttp3 still has method

Builder method(String method, RequestBody body)

So OPTIONS requests looks like

        Request request = new Request.Builder()
                .url(url)
                .headers(headerBuilder.build())
                .method("OPTIONS",requestBody)
                .build();

same for HEAD

Upvotes: 12

Related Questions