### Installing Async::HTTP Gem (Bash) Source: https://github.com/socketry/async-http/blob/main/guides/getting-started/readme.md This snippet demonstrates how to add the `async-http` gem to your Ruby project using Bundler. It's a standard command-line operation for dependency management. ```bash $ bundle add async-http ``` -------------------------------- ### Making HTTP/2 Request with Curl (Bash) Source: https://github.com/socketry/async-http/blob/main/examples/hello/readme.md This snippet illustrates how to make an HTTP/2 GET request to the server using curl with the --http2-prior-knowledge flag. The verbose output details the HTTP/2 stream setup, request headers, and the 'Hello World!' response, demonstrating HTTP/2 compatibility. ```bash $ curl -v --http2-prior-knowledge http://localhost:3000 * Host localhost:3000 was resolved. * IPv6: ::1 * IPv4: 127.0.0.1 * Trying [::1]:3000... * Connected to localhost (::1) port 3000 * [HTTP/2] [1] OPENED stream for http://localhost:3000/ * [HTTP/2] [1] [:method: GET] * [HTTP/2] [1] [:scheme: http] * [HTTP/2] [1] [:authority: localhost:3000] * [HTTP/2] [1] [:path: /] * [HTTP/2] [1] [user-agent: curl/8.7.1] * [HTTP/2] [1] [accept: */*] > GET / HTTP/2 > Host: localhost:3000 > User-Agent: curl/8.7.1 > Accept: */* > * Request completely sent off < HTTP/2 200 < content-length: 12 < vary: accept-encoding < * Connection #0 to host localhost left intact Hello World!⏎ ``` -------------------------------- ### Creating and Running an Async::HTTP Server (Ruby) Source: https://github.com/socketry/async-http/blob/main/guides/getting-started/readme.md This Ruby snippet illustrates how to set up and run a basic HTTP server using `Async::HTTP::Server`. It defines an endpoint, starts the server in a transient task, and then demonstrates making a client request to that server, ensuring the client response is closed. ```ruby require 'async/http' endpoint = Async::HTTP::Endpoint.parse('http://localhost:9292') Sync do |task| Async(transient: true) do server = Async::HTTP::Server.for(endpoint) do |request| ::Protocol::HTTP::Response[200, {}, ["Hello World"]] end server.run end client = Async::HTTP::Client.new(endpoint) response = client.get("/") puts response.read ensure response&.close end ``` -------------------------------- ### Setting Up Async-HTTP Server with Falcon (Bash) Source: https://github.com/socketry/async-http/blob/main/examples/hello/readme.md This snippet provides the bash commands required to update dependencies and start the async-http server using Falcon. It binds the server to http://localhost:3000, making it accessible for client requests. ```bash $ bundle update $ bundle exec falcon serve --bind http://localhost:3000 ``` -------------------------------- ### Making a GET Request with Async::HTTP::Internet (Ruby) Source: https://github.com/socketry/async-http/blob/main/guides/getting-started/readme.md This Ruby snippet shows how to perform a GET request using `Async::HTTP::Internet.get`. It utilizes a block to automatically close the response, ensuring resources are released after reading the body. ```ruby require 'async/http/internet/instance' Sync do Async::HTTP::Internet.get("https://httpbin.org/get") do |response| puts response.read end end ``` -------------------------------- ### Listing Supported HTTP Methods for Async::HTTP::Internet (Ruby) Source: https://github.com/socketry/async-http/blob/main/guides/getting-started/readme.md This Ruby snippet demonstrates how to retrieve a list of HTTP methods supported by the `Async::HTTP::Internet` class. It calls the `methods(false)` method to get only instance methods, excluding inherited ones. ```ruby Async::HTTP::Internet.methods(false) # => [:patch, :options, :connect, :post, :get, :delete, :head, :trace, :put] ``` -------------------------------- ### Setting a Request Timeout with Async::HTTP (Ruby) Source: https://github.com/socketry/async-http/blob/main/guides/getting-started/readme.md This Ruby snippet demonstrates how to apply a timeout to an HTTP request using `Task#with_timeout`. It wraps the request in a timeout block, catching `Async::TimeoutError` if the request exceeds the specified duration (2 seconds in this example). ```ruby require 'async/http/internet/instance' Sync do |task| # Request will timeout after 2 seconds task.with_timeout(2) do response = Async::HTTP::Internet.get "https://httpbin.org/delay/10" ensure response&.close end rescue Async::TimeoutError puts "The request timed out" end ``` -------------------------------- ### Making a GET Request with Manual Response Closing (Ruby) Source: https://github.com/socketry/async-http/blob/main/guides/getting-started/readme.md This Ruby snippet illustrates how to make a GET request and manually manage the response lifecycle. It's crucial to call `response&.close` in an `ensure` block to prevent resource leaks when not using a block-based approach. ```ruby require 'async/http/internet/instance' Sync do response = Async::HTTP::Internet.get("https://httpbin.org/get") puts response.read ensure response&.close end ``` -------------------------------- ### Downloading a File with Async::HTTP::Internet (Ruby) Source: https://github.com/socketry/async-http/blob/main/guides/getting-started/readme.md This Ruby snippet demonstrates how to download content from a URL and save it to a local file. It uses `Async::HTTP::Internet.get` to fetch the response and `response.save` to write the body to the specified path, ensuring the response is closed afterwards. ```ruby require 'async/http/internet/instance' Sync do # Issue a GET request to Google: response = Async::HTTP::Internet.get("https://www.google.com/search?q=kittens") # Save the response body to a local file: response.save("/tmp/search.html") ensure response&.close end ``` -------------------------------- ### Making HTTP/1 Request with Curl (Bash) Source: https://github.com/socketry/async-http/blob/main/examples/hello/readme.md This snippet demonstrates how to make an HTTP/1 GET request to the running server using curl. It shows the verbose output, including request headers, response headers, and the 'Hello World!' body, confirming successful communication. ```bash $ curl -v http://localhost:3000 * Host localhost:3000 was resolved. * IPv6: ::1 * IPv4: 127.0.0.1 * Trying [::1]:3000... * Connected to localhost (::1) port 3000 > GET / HTTP/1.1 > Host: localhost:3000 > User-Agent: curl/8.7.1 > Accept: */* > * Request completely sent off < HTTP/1.1 200 OK < vary: accept-encoding < content-length: 12 < * Connection #0 to host localhost left intact Hello World!⏎ ``` -------------------------------- ### Posting JSON Data with Async::HTTP::Internet (Ruby) Source: https://github.com/socketry/async-http/blob/main/guides/getting-started/readme.md This Ruby snippet shows how to perform a POST request with JSON data. It prepares headers and a JSON-encoded body, sends the request using `Async::HTTP::Internet.post`, and then parses and prints the JSON response, ensuring the response is closed. ```ruby require 'async/http/internet/instance' data = {'life' => 42} Sync do # Prepare the request: headers = [['accept', 'application/json']] body = JSON.dump(data) # Issues a POST request: response = Async::HTTP::Internet.post("https://httpbin.org/anything", headers, body) # Save the response body to a local file: pp JSON.parse(response.read) ensure response&.close end ``` -------------------------------- ### Mocking HTTP Responses with Async::HTTP::Mock::Endpoint (Ruby) Source: https://github.com/socketry/async-http/blob/main/guides/testing/readme.md This snippet demonstrates how to set up a mock HTTP server using `Async::HTTP::Mock::Endpoint`. It starts a background server that intercepts requests and returns a predefined response, allowing the use of a real HTTP client against a mocked endpoint. This avoids making actual network calls during testing. ```Ruby require 'async/http' require 'async/http/mock' mock_endpoint = Async::HTTP::Mock::Endpoint.new Sync do # Start a background server: server_task = Async(transient: true) do mock_endpoint.run do |request| # Respond to the request: ::Protocol::HTTP::Response[200, {}, ["Hello, World"]] end end endpoint = Async::HTTP::Endpoint.parse("https://www.google.com") mocked_endpoint = mock_endpoint.wrap(endpoint) client = Async::HTTP::Client.new(mocked_endpoint) response = client.get("/") puts response.read # => "Hello, World" end ``` -------------------------------- ### Testing HTTP/1.1 Maximum Line Length with Curl Source: https://github.com/socketry/async-http/blob/main/releases.md This `curl` command sends a GET request with a URL path exceeding the configured `maximum_line_length` of 32 bytes. The expected outcome is an 'Empty reply from server' error, indicating that the server rejected the request due to the line length limit. ```Shell curl -v http://localhost:9292/012345678901234567890123456789012 ``` -------------------------------- ### Transparent HTTP Client Mocking with Sus and Async::HTTP (Ruby) Source: https://github.com/socketry/async-http/blob/main/guides/testing/readme.md This example shows how to integrate `Async::HTTP` mocking with the Sus testing framework. It uses Sus's `mock` feature to replace the `Async::HTTP::Client#new` method, ensuring that any new client instances automatically use a mocked endpoint. A background mock server is set up to provide consistent responses for tests. ```Ruby require 'async/http' require 'async/http/mock' require 'sus/fixtures/async/reactor_context' include Sus::Fixtures::Async::ReactorContext let(:mock_endpoint) {Async::HTTP::Mock::Endpoint.new} def before super # Mock the HTTP client: mock(Async::HTTP::Client) do |mock| mock.wrap(:new) do |original, endpoint| original.call(mock_endpoint.wrap(endpoint)) end end # Run the mock server: Async(transient: true) do mock_endpoint.run do |request| ::Protocol::HTTP::Response[200, {}, ["Hello, World"]] end end end it "should perform a web request" do client = Async::HTTP::Client.new(Async::HTTP::Endpoint.parse("https://www.google.com")) response = client.get("/") # The response is mocked: expect(response.read).to be == "Hello, World" end ``` -------------------------------- ### Testing Non-Persistent HTTP/1.1 Connection with Curl Source: https://github.com/socketry/async-http/blob/main/releases.md This `curl` command sends a GET request to the locally running HTTP server configured with `persistent: false`. The verbose output (`-v`) confirms that the connection is closed immediately after the response is received, demonstrating the effect of the `persistent: false` protocol option. ```Shell curl -v http://localhost:9292 ``` -------------------------------- ### Overriding Request Authority with Async::HTTP::Internet in Ruby Source: https://github.com/socketry/async-http/blob/main/releases.md This Ruby snippet shows how to use `Async::HTTP::Internet` to make a GET request while overriding the target authority. By passing the `authority` keyword argument, the client can specify a different host for the request's `Host` header (HTTP/1.1) or `:authority` pseudo-header (HTTP/2) than the one derived from the URL. ```Ruby internet = Async::HTTP::Internet.instance # This will let you override the authority (HTTP/1.1 host header, HTTP/2 :authority header): internet.get("https://proxy.local", authority: "example.com") ``` -------------------------------- ### Configuring HTTP/1.1 Protocol Options in Ruby Source: https://github.com/socketry/async-http/blob/main/releases.md This snippet demonstrates how to configure an `Async::HTTP::Protocol::HTTP1` instance with custom options, specifically disabling persistent connections (`persistent: false`) and setting a maximum line length (`maximum_line_length: 32`). It then uses this configured protocol to set up a basic HTTP server that responds with 'Hello, world'. ```Ruby protocol = Async::HTTP::Protocol::HTTP1.new(persistent: false, maximum_line_length: 32) endpoint = Async::HTTP::Endpoint.parse("http://localhost:9292", protocol: protocol) server = Async::HTTP::Server.for(endpoint) do |request| Protocol::HTTP::Response[200, {}, ["Hello, world"]] end.run ``` -------------------------------- ### Performing a CONNECT Request with Async::HTTP in Ruby Source: https://github.com/socketry/async-http/blob/main/releases.md This Ruby snippet demonstrates how to initiate a `CONNECT` request using an `Async::HTTP` client. The target authority (host and port) for the connection is specified via the `authority` keyword argument, which is correctly mapped to the appropriate HTTP/1 or HTTP/2 header. ```Ruby response = client.connect(authority: "example.com:443") ``` -------------------------------- ### HTTP/2 CONNECT Method Pseudo-Headers Source: https://github.com/socketry/async-http/blob/main/releases.md This snippet illustrates the pseudo-headers used for an HTTP/2 `CONNECT` request. Unlike HTTP/1.1, HTTP/2 uses the `:authority` pseudo-header to specify the target host and port for the connection, rather than including it in the request line. ```HTTP [HEADERS FRAME] :method: connect :authority: example.com:443 ``` -------------------------------- ### Handling Interim 100-Continue Responses on Server in Ruby Source: https://github.com/socketry/async-http/blob/main/releases.md This Ruby server-side snippet illustrates how to implement an `interim_response` callback within a request handler. It checks for the '100-continue' expectation header and, if present, sends a 100 (Continue) interim response back to the client, allowing the client to proceed with sending the request body. ```Ruby def call(request) if request.headers['expect'].include?('100-continue') request.send_interim_response(100) end # ... end ``` -------------------------------- ### Sending Asynchronous HTTP POST Request with Writable Body in Ruby Source: https://github.com/socketry/async-http/blob/main/releases.md This snippet demonstrates how to perform an asynchronous HTTP POST request using `Async::HTTP` with a writable request body. It shows how to handle an `interim_response` (specifically a `100 Continue` status) to write data to the body and how to close the body upon successful or unsuccessful responses. This pattern is useful for streaming data or handling large uploads. ```Ruby body = Async::HTTP::Body::Writable.new interim_repsonse = proc do |status, headers| if status == 100 # Continue sending the body... body.write("Hello, world!") body.close end end Async::HTTP::Internet.instance.post("https://example.com", body, interim_response: interim_repsonse) do |response| unless response.success? body.close end end ``` -------------------------------- ### Overriding Request Scheme with Async::HTTP::Internet in Ruby Source: https://github.com/socketry/async-http/blob/main/releases.md This Ruby snippet demonstrates how to override the scheme of an HTTP request when using `Async::HTTP::Internet`. Even if the URL specifies HTTPS, the `scheme: 'http'` keyword argument forces the request to be treated as an HTTP request, affecting how the client constructs the request. ```Ruby internet.get("https://example.com", scheme: "http") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.