From 62a178f0da1627f740fed2da9f3d154228414e01 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Sat, 22 May 2021 17:18:18 +0900 Subject: [PATCH] post --- main.cr | 10 +++++----- src/mechanize.cr | 2 +- src/mechanize/http/agent.cr | 4 +++- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/main.cr b/main.cr index 011b24b..c5c64f2 100644 --- a/main.cr +++ b/main.cr @@ -2,11 +2,11 @@ require "./src/mechanize.cr" agent = Mechanize.new agent.request_headers = HTTP::Headers{"Foo" => "Bar"} -params = {"hoge" => "hoge"} -page = agent.get("http://google.com/", params: params) -#query = {"foo" => "foo_value", "bar" => "bar_value"} -#page = agent.post("http//example.com/", query: query) +#params = {"hoge" => "hoge"} +#page = agent.get("http://example.com/", params: params) +query = {"foo" => "foo_value", "bar" => "bar_value"} +page = agent.post("http://example.com/", query: query) #puts page.code -#puts page.body +puts page.body #puts page.css("h1").first.inner_text #puts page.title diff --git a/src/mechanize.cr b/src/mechanize.cr index 9da3313..866d132 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -45,7 +45,7 @@ class Mechanize headers.merge!(content_headers) # fetch the page - page = @agent.fetch uri, :post, headers, [request_data]#, cur_page + page = @agent.fetch(uri, :post, headers: headers, params: {"value" => request_data })#, cur_page #add_to_history(page) page end diff --git a/src/mechanize/http/agent.cr b/src/mechanize/http/agent.cr index 5f89567..ca90fc6 100644 --- a/src/mechanize/http/agent.cr +++ b/src/mechanize/http/agent.cr @@ -30,7 +30,7 @@ module MechanizeCr when :get ::HTTP::Client.get(uri, headers: request_headers) when :post - #client.post(path) + ::HTTP::Client.post(uri, headers: request_headers, form: params.not_nil!.fetch("value", "")) end end end @@ -47,6 +47,8 @@ module MechanizeCr query = URI::Params.encode(params) uri.query = query return uri, nil + when :post + return uri, params else return uri, params end