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

View File

@ -4,6 +4,7 @@ require "./form/check_box"
class MechanizeCr::Form class MechanizeCr::Form
getter fields : Array(MechanizeCr::FormContent::Field) getter fields : Array(MechanizeCr::FormContent::Field)
getter checkboxes : Array(MechanizeCr::FormContent::CheckBox) getter checkboxes : Array(MechanizeCr::FormContent::CheckBox)
getter enctype : String
def initialize(node : Node) def initialize(node : Node)
@enctype = node["enctype"] || "application/x-www-form-urlencoded" @enctype = node["enctype"] || "application/x-www-form-urlencoded"
@ -24,10 +25,11 @@ class MechanizeCr::Form
def request_data def request_data
query_params = build_query query_params = build_query
build_query_string(query_params)
end end
def build_query() def build_query
query = [] of String query = [] of Array(String)
successful_controls = Array(MechanizeCr::FormContent::Field | MechanizeCr::FormContent::CheckBox).new successful_controls = Array(MechanizeCr::FormContent::Field | MechanizeCr::FormContent::CheckBox).new
fields.each do |elm| fields.each do |elm|
successful_controls << elm successful_controls << elm
@ -37,6 +39,11 @@ class MechanizeCr::Form
successful_controls << elm successful_controls << elm
end end
end end
successful_controls.each do |ctrl|
value = ctrl.query_value
query.push(value)
end
query
end end
def parse def parse
@ -45,4 +52,11 @@ class MechanizeCr::Form
@node.search("input").not_nil!.each do |node| @node.search("input").not_nil!.each do |node|
end end
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 end

View File

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