diff --git a/src/mechanize.cr b/src/mechanize.cr index a2d2b66..1d0bc09 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -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 diff --git a/src/mechanize/form.cr b/src/mechanize/form.cr index e99d3c1..a8a7e67 100644 --- a/src/mechanize/form.cr +++ b/src/mechanize/form.cr @@ -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 diff --git a/src/mechanize/form/field.cr b/src/mechanize/form/field.cr index 7615ac2..8c2d4f1 100644 --- a/src/mechanize/form/field.cr +++ b/src/mechanize/form/field.cr @@ -9,6 +9,6 @@ class MechanizeCr::FormContent::Field end def query_value - [[@name, @value || ""]] + [@name, @value || ""] end end