Mechanize
Inherits Reference / Object
This class is main class of Mechanize.cr, using this class' instance to start web interaction.
Examples:
# GET
agent = Mechanize.new
# send GET request to http://example.com/?foo=bar
agent.get("http://example.com",
params: {"foo" => "bar"},
headers: HTTP::Headers{"Foo" => "Bar"})
# POST
# send POST request whose post body is "foo=bar"
agent = Mechanize.new
agent.post("http://example.com",
query: {"foo" => "bar"})
Constants
Constructors
Instance methods
set basic auth credentials.
# set an auth credential with a specific url.
agent.add_auth("http://example.com", "username", "password")
add page to history (Mechanize::History).
if you send request, mechanize calls this method and records page, so you don't need to call this on your own.
get the latest page recorded in history, and the page is deleted from history.
agent.back => #<Mechanize::Page>
click link, and transit page.
page = agent.get("http://example.com")
link = page.links.first
page2 = agent.click(link)
Send DELETE request to specified uri with headers, and body.
Examples (send delete request whose body is "hello")
agent = Mechanize.new
agent.delete("http://example.com",
body: "hello")
download page body from given uri.
# make download.html whose content is http://example.com's html.
agent.download("http://example.com", "download.html")
Send GET request to specified uri with headers, and parameters.
Examples (send request to http://example.com/?foo=bar)
agent = Mechanize.new
agent.get("http://example.com",
params: {"foo" => "bar"},
headers: HTTP::Headers{"Foo" => "Bar"})
Send HEAD request to specified uri with headers.
Examples (send HEAD request to http://example.com/)
agent = Mechanize.new
agent.head("http://example.com")
get the history (Mechanize::History).
the requests mechanize send is recorded in this history.
agent.history => #<Mechanize::History>
Get maximum number of pages allowed in the history. The default setting is 100 pages.
agent.max_history # => 100
set maximum number of pages allowed in the history. the default value is 100.
agent.max_history = 150
Send POST request to specified uri with headers, and query.
Examples (send post request whose post body is "foo=bar")
agent = Mechanize.new
agent.post("http://example.com",
query: {"foo" => "bar"},
headers: HTTP::Headers{"Foo" => "Bar"})
Send PUT request to specified uri with headers, and body.
Examples (send put request whose body is "hello")
agent = Mechanize.new
agent.put("http://example.com",
body: "hello")
get the value of request headers.
agent.request_headers # => HTTP::Headers{"Foo" => "Bar"}
set the value of request headers.
agent.request_headers = HTTP::Headers{"Foo" => "Bar"}
send request from form. 'form' args is necessary, but 'button' is optional. you can specify button if you want to use other button which is not in the form. TODO: now only supports POST. other HTTP methods should be supported.
page = agent.get("http://example.com")
form = page.forms[0]
# set field value
form.field_with("foo").value = "bar"
agent.submit(form)
get the value of user agent.
agent.user_agent # => "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; NP06; rv:11.0) like Gecko"
set the value of user agent.
agent.user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; NP06; rv:11.0) like Gecko"