request_data

master
Kanezoh 2021-05-20 18:00:39 +09:00
parent 16e4c7aed3
commit 890d4e5449
3 changed files with 24 additions and 10 deletions

View File

@ -29,7 +29,7 @@ class Mechanize
node["name"] = k
form.fields << MechanizeCr::FormContent::Field.new(node, v)
end
#post_form(uri, form, headers)
post_form(uri, form, headers)
end
def post_form(uri, form, headers)
@ -37,16 +37,16 @@ class Mechanize
# Page.new
request_data = form.request_data
headers = {
content_headers = ::HTTP::Headers{
"Content-Type" => form.enctype,
"Content-Length" => request_data.size.to_s,
}.merge headers
}
headers.merge!(content_headers)
# fetch the page
page = @agent.fetch uri, :post, headers, [request_data], cur_page
add_to_history(page)
page
#page = @agent.fetch uri, :post, headers, [request_data]#, cur_page
#add_to_history(page)
#page
end
def request_headers

View File

@ -4,6 +4,7 @@ require "./form/check_box"
class MechanizeCr::Form
getter fields : Array(MechanizeCr::FormContent::Field)
getter checkboxes : Array(MechanizeCr::FormContent::CheckBox)
getter enctype : String
def initialize(node : Node)
@enctype = node["enctype"] || "application/x-www-form-urlencoded"
@ -24,10 +25,11 @@ class MechanizeCr::Form
def request_data
query_params = build_query
build_query_string(query_params)
end
def build_query()
query = [] of String
def build_query
query = [] of Array(String)
successful_controls = Array(MechanizeCr::FormContent::Field | MechanizeCr::FormContent::CheckBox).new
fields.each do |elm|
successful_controls << elm
@ -37,6 +39,11 @@ class MechanizeCr::Form
successful_controls << elm
end
end
successful_controls.each do |ctrl|
value = ctrl.query_value
query.push(value)
end
query
end
def parse
@ -45,4 +52,11 @@ class MechanizeCr::Form
@node.search("input").not_nil!.each do |node|
end
end
def build_query_string(params : Array(Array(String)))
params.reduce("") do |acc, arr|
hash = { arr[0] => arr[1] }
acc + URI::Params.encode(hash) + '&'
end.rchop
end
end

View File

@ -9,6 +9,6 @@ class MechanizeCr::FormContent::Field
end
def query_value
[[@name, @value || ""]]
[@name, @value || ""]
end
end