post form

master
Kanezoh 2021-05-18 18:46:43 +09:00
parent 085c94b93c
commit da90024a96
3 changed files with 25 additions and 8 deletions

View File

@ -4,7 +4,8 @@ agent = Mechanize.new
agent.request_headers = HTTP::Headers{"Foo" => "Bar"}
params = {"hoge" => "hoge"}
#page = agent.get("http://example.com/", params: params)
page = agent.post("http//example.com/")
query = {"foo" => "foo_value", "bar" => "bar_value"}
page = agent.post("http//example.com/", query: query)
#puts page.code
#puts page.body
#puts page.css("h1").first.inner_text

View File

@ -23,7 +23,27 @@ class Mechanize
node["enctype"] = "application/x-www-form-urlencoded"
form = MechanizeCr::Form.new(node)
form.fields << MechanizeCr::FormContent::Field.new({"name" => "foo"}, "bar")
query.each do |k,v|
form.fields << MechanizeCr::FormContent::Field.new({"name" => k}, v)
end
#post_form(uri, form, headers)
end
def post_form(uri, form, headers)
#cur_page = form.page || current_page ||
# Page.new
request_data = form.request_data
headers = {
"Content-Type" => form.enctype,
"Content-Length" => request_data.size.to_s,
}.merge headers
# fetch the page
page = @agent.fetch uri, :post, headers, [request_data], cur_page
add_to_history(page)
page
end
def request_headers

View File

@ -1,14 +1,10 @@
class MechanizeCr::FormContent::Field
property :node, :value
def initialize(node : Hash(String, String), value : String = node["value"])
@node = node
#@name = Mechanize::Util.html_unescape(node['name'])
#@raw_value = value
#@value = if value.is_a? String
# Mechanize::Util.html_unescape(value)
# else
# value
# end
#
@value = value
#@type = node['type']
end
end