How to enforce Faraday adapter typhoeus to use HTTP/2 for requests to servers which supported HTTP/2?
I have tested this over service https://http2.pro/doc/api and result was like this:
body="{\"http2\":1,\"protocol\":\"HTTP\\/2.0\",\"push\":0,\"user_agent\":\"Faraday v0.12.2\"}",
\"http2\":1, what means that HTTP/2 not used for request!
There are two things at play here. The first is that the remote API is lying to you in the response body. Their documentation says:
Even though the response body shows
'http2': 1indicating that HTTP2 was not used, it is being used. You can most easily confirm this using Chrome's dev tools:So once we know that the API is lying in the response body, how can we independently confirm that Typhoeus is using HTTP2?
(this answer assumes you are using
pryas your REPL, not IRB)First let's confirm that Typhoeus alone will use HTTP2:
Now let's test it in Faraday:
But how can we confirm it was HTTP2? This doesn't work:
Because
responseisn't aTyphoeus::Responseobject, it's a Faraday object:So we need to get into the gem itself to figure out where it's creating the
Typhoeus::Responseobject so we can call.http_versionon it manually and confirm it's using the protocol we expect. As it turns out, that's right here.Let's take the easy route and stick
binding.pryinto our local copy of the gem (you'll need to restart pry to pick up the changes to the gem):Then re-run the request:
And you'll see:
Now enter:
And confirm it's a
Typhoeus::Responseobject:And confirm it's using HTTP2:
And confirm the API response body is a dirty liar:
And that's how you use Typhoeus as a Faraday adapter to make an HTTP2 request.