File: request.rb

package info (click to toggle)
ruby-async-http 0.59.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 572 kB
  • sloc: ruby: 4,164; javascript: 40; makefile: 4
file content (37 lines) | stat: -rw-r--r-- 811 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env ruby
# 
# $LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
# $LOAD_PATH.unshift(File.expand_path("../../http-protocol/lib", __dir__))

require 'async'
require 'async/http/client'
require 'async/http/endpoint'

# Console.logger.level = Logger::DEBUG

Async do |task|
	endpoint = Async::HTTP::Endpoint.parse("https://www.google.com")
	
	client = Async::HTTP::Client.new(endpoint)
	
	headers = {
		'accept' => 'text/html',
	}
	
	request = Protocol::HTTP::Request.new(client.scheme, "www.google.com", "GET", "/search?q=cats", headers)
	
	puts "Sending request..."
	response = client.call(request)
	
	puts "Reading response status=#{response.status}..."
	
	if body = response.body
		while chunk = body.read
			puts chunk.size
		end
	end
	
	response.close
	
	puts "Finish reading response."
end