Skip to content

reqvsmechanize

MIT 94 1 4,781
58.1 thousand (month) Nov 20 2023 v3.57.0(2025-12-16 09:07:40 ago)
4,440 8 6 MIT
Jul 25 2009 213.1 thousand (month) 2.14.0(2025-01-05 18:30:46 ago)

The Go library "req" is a simple and easy-to-use library for making HTTP requests in Go. It is designed to make working with HTTP requests as simple as possible, by providing a clean and consistent API for handling various types of requests, including GET, POST, PUT, and DELETE.

One of the key features of req is its support for handling JSON data. The library automatically serializes and deserializes JSON data, making it easy to work with JSON data in your Go applications. Additionally, it supports multipart file uploads and automatic decompression of gzip and deflate encoded responses.

req also includes a number of convenience functions for working with common HTTP request types, such as sending GET and POST requests, handling redirects, and setting headers and query parameters. The library can also be easily extended with custom middleware and request handlers.

Overall, req is a powerful and flexible library that makes it easy to work with HTTP requests in Go. It is well-documented and actively maintained, making it a great choice for any Go project that needs to work with HTTP requests.

Mechanize is a Ruby library for automating interaction with websites. It automatically stores and sends cookies, follows redirects, and can submit forms — making it behave like a web browser without needing an actual browser engine.

Key features include:

  • Automatic cookie management Stores cookies received from servers and sends them back on subsequent requests, maintaining session state across multiple pages.
  • Form handling Can find, fill in, and submit HTML forms programmatically. Supports text inputs, selects, checkboxes, radio buttons, and file uploads.
  • Link following Navigate through pages by clicking links using their text content, CSS selectors, or href patterns.
  • History and back/forward Maintains a browsing history, allowing you to go back and forward through visited pages.
  • HTTP authentication Supports basic and digest HTTP authentication.
  • Proxy support Can route requests through HTTP proxies.
  • Redirect handling Automatically follows HTTP redirects (configurable).

Mechanize is one of the oldest and most established web interaction libraries in Ruby. It is best suited for scraping traditional server-rendered websites with forms and multi-page workflows. For JavaScript-heavy sites, a browser automation tool like Selenium or Playwright is recommended instead.

Highlights


popularproduction

Example Use


```go req.DevMode() // Use Client.DevMode to enable debugging details // There are 2 ways to use req (like many other http clients) // First way is to create a persistent session client: client := req.C(). // defaults like timeout and headers can be set for the whole session SetUserAgent("my-custom-client"). SetTimeout(5 * time.Second) // defaults can be overriden and extended in each request resp, err := client.R(). // Use R() to create a request and set with chainable request settings. SetHeader("Accept", "application/vnd.github.v3+json"). SetPathParam("username", "imroc"). SetQueryParam("page", "1"). SetResult(&result). // Unmarshal response into struct automatically if status code >= 200 and <= 299. SetError(&errMsg). // Unmarshal response into struct automatically if status code >= 400. EnableDump(). // Enable dump at request level to help troubleshoot, log content only when an unexpected exception occurs. Get("https://api.github.com/users/{username}/repos") // Alternatively, it can be used as is without establishing a client resp := client.Get("https://api.github.com/users/{username}/repos"). // Create a GET request with specified URL. SetHeader("Accept", "application/vnd.github.v3+json"). SetPathParam("username", "imroc"). SetQueryParam("page", "1"). SetResult(&result). SetError(&errMsg). EnableDump(). Do() // Send request with Do. ```
```ruby require 'mechanize' agent = Mechanize.new # Navigate to a page page = agent.get('https://example.com') puts page.title # Find and click a link page = page.link_with(text: 'Products').click # Extract data from the page page.search('.product').each do |product| name = product.at('.name').text price = product.at('.price').text puts "#{name}: #{price}" end # Fill in and submit a login form login_page = agent.get('https://example.com/login') form = login_page.form_with(action: '/login') form['username'] = 'user@example.com' form['password'] = 'password123' dashboard = agent.submit(form) # Cookies are maintained automatically puts dashboard.title # "Dashboard" # Download a file agent.get('https://example.com/report.csv').save('report.csv') ```

Alternatives / Similar


Was this page helpful?