treat uri as URI

This commit is contained in:
Kanezoh 2021-05-03 13:28:34 +09:00
parent 803e242035
commit 3f39c9d9bd
2 changed files with 3 additions and 4 deletions

View File

@ -2,7 +2,7 @@ require "http/client"
class MechanizeCr::File class MechanizeCr::File
#property :body, :filename #property :body, :filename
property :body, :code property :body, :code
def initialize(uri : String | Nil, response : ::HTTP::Client::Response | Nil, body : String , code : Int32 | Nil) def initialize(uri : URI | Nil, response : ::HTTP::Client::Response | Nil, body : String , code : Int32 | Nil)
@uri = uri @uri = uri
@body = body @body = body
@code = code @code = code

View File

@ -12,16 +12,15 @@ module MechanizeCr
end end
def fetch(uri, method = :get, headers = HTTP::Headers.new, params = Hash(String,String).new) def fetch(uri, method = :get, headers = HTTP::Headers.new, params = Hash(String,String).new)
uri = URI.parse(uri)
add_request_headers(headers) add_request_headers(headers)
params = hash_to_params(params) params = hash_to_params(params)
response = http_request uri, method, params response = http_request uri, method, params
body = response.not_nil!.body body = response.not_nil!.body
page = response_parse(response, body, uri) page = response_parse(response, body, uri)
puts page.code
end end
def http_request(uri, method, params) def http_request(uri, method, params)
uri = URI.parse(uri)
request = ::HTTP::Client.new(uri.host.not_nil!) request = ::HTTP::Client.new(uri.host.not_nil!)
path = compose_path(uri, params) path = compose_path(uri, params)
case uri.scheme.not_nil!.downcase case uri.scheme.not_nil!.downcase