OPTIONS/HEAD REST API request with Okhttp3 - java

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?

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

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()

Related

Add Proxy to HttpRequest in java

I'm trying to understand how can I implement the use of a proxy for each request built like the following using Java API:
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.version(HttpClient.Version.HTTP_2)
.uri(URI.createh("https://myurl"))
.timeout(Duration.ofMinutes(2))
.setHeader("User-Agent","Just an user agent")
.GET()
.build();
HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString());
I'm seeing from the doc (https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpClient.html#Asynchronous%20Example)
that is possible with Synchronous requests. My code is within a method and it will run with threads parallelly. So how is it possible to set a proxy with Asynchronous Requests? If it is not possible, what's the difference between them?
Solved, it's a bit unclear the doc about that but at the end, I was able to set the proxy when building the client:
HttpClient client = HttpClient.newBuilder().
proxy(ProxySelector.of(new InetSocketAddress("proxy",port))))
.build();
//The request code is identical to what I wrote above.
The method is newBuilder anyway and not Builder.

What's the simplest way to call a SOAP web service using a String for the request and response?

I'm diagnosing an issue and would like to know the simplest way to call a SOAP web service. I'd like to setup a simple junit (integration) test that will hit a SOAP service. I have a SoapUI request working as intended, I'd like to take the URL and soap envelope XML request as a String from that, call it from a Java class and have the response as a String. I'm trying to avoid generating all of the objects/clients/etc usually involved with working with a SOAP service. I'm just looking for a quick and dirty way to accomplish this.
I found that the following meets my needs:
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(SERVICE_URL)
.put(RequestBody.create(MediaType.parse("application/octet-stream"), REQUEST_BODY))
.addHeader("cache-control", "no-cache")
.build();
Response response = client.newCall(request).execute();
String responseBody = response.body().string();

why am I getting a PROTOCOL ERROR from OkHttp request?

I've been searching for awhile on this topic. I'm trying to send data to my backend server using OkHttp. However, I am getting this error:
okhttp3.internal.http2.StreamResetException: stream was reset:PROTOCOL_ERROR
val client = OkHttpClient()
val response = client.newCall(
Request.Builder()
.addHeader("Authorization:", "Bearer $firebaseToken")
.url("https://someURL/"+ podcastId.toString())
.build())
.execute()
I'm not sure if this has to do with the URL or my Authorization header? Any help would be appreciated, thanks in advance.
I solved a similar problem by restricting the request to use HTTP 1, see:
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setProtocols(Arrays.asList(Protocol.HTTP_1_1));

Passing Multiple URL's for OKhttp url Request type

Is there any way to Passing Multiple URL's for OKhttp url Request type. Actually i want pass multi url's like website, mobile request urls(Androd, mobile browser, tablet)
Request request = new Request.Builder()
.url("multi url" + ---
.addheader(---).build();
No
The OkHttp Request object allows one and only one URL.
If you require several requests, create different requests:
Request stackoverflowRequest = new Request.Builder()
.url("https://www.stackoverflow.com/")
.addHeader(...)
.build();
Request googleRequest= new Request.Builder()
.url("https://www.google.com/")
.addHeader(...)
.build();
If you don't want to write all the headers X times, you can use the following:
Request templateRequest = new Request.Builder()
.url("https://www.example.com/")
.addHeader(...)
.build();
Request stackoverflowRequest = templateRequest.newBuilder()
.url("https://www.stackoverflow.com/")
.build();
Request googleRequest = templateRequest.newBuilder()
.url("https://www.google.com/")
.build();

Okhttp: Adding unencrypted custom headers in for Proxy server on https requests

We are using okhttp v3.8.0 in our project. We have to add custom header specifically for proxy server on https requests. The issues is that when i set ".header("Something", "FRR")", header would be encrypted on https requests as well, so it would not be identified by Proxy server. How can I achieve that? I want to send the header unencrypted in Initial method.
That's how I send my request to proxy server right now:
OK_HTTP_CLIENT = builder
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.cookieJar(cookieJar)
.retryOnConnectionFailure(true)
.proxy(proxy)
.proxyAuthenticator(proxyAuthenticatorMainAccount)
.build();
Request request = new Request.Builder()
.url(url)
.header("Something", "FRR")
.build();
Response response = OK_HTTP_CLIENT.newCall(request).execute();
There is screenshot here, explain what i want to achieve in more details
This isn't possibly in OkHttp currently. It's being tracked here. If it's important to you, please explain your situation and we’ll respond accordingly.

Categories

Resources