# Ignore Request

By default, VCR hooks into every request, either allowing it and recording it, or playing back a recorded response, or raising an error to force you to deal with the new request. In some situations, you may prefer to have VCR ignore some requests.

VCR provides 3 configuration options to accomplish this:

* `ignore_request { |req| ... }` will ignore any request for which the

  given block returns true.
* `ignore_hosts 'foo.com', 'bar.com'` allows you to specify particular

  hosts to ignore.
* `ignore_localhost = true` is equivalent to \`ignore\_hosts 'localhost',

  '127.0.0.1', '0.0.0.0'\`. It is particularly useful for when you use

  VCR with a javascript-enabled capybara driver, since capybara boots

  your rack app and makes localhost requests to it to check that it has

  booted.
* `unignore_hosts 'foo.com', 'bar.com'` makes VCR stop ignoring particular hosts.

  Ignored requests are not recorded and are always allowed, regardless of the record mode, and even outside of a `VCR.use_cassette` block.

## Background ()

*Given* a file named "sinatra\_app.rb" with:

```
response_count = 0
$server = start_sinatra_app do
  get('/') { "Port 7777 Response #{response_count += 1}" }
end
```

## ignore requests to a specific port

*Given* a file named "ignore\_request.rb" with:

```
include_http_adapter_for("<http_lib>")
require 'sinatra_app.rb'

response_count = 0
$server_8888 = start_sinatra_app do
  get('/') { "Port 8888 Response #{response_count += 1}" }
end

require 'vcr'

VCR.configure do |c|
  c.ignore_request do |request|
    URI(request.uri).port == $server.port
  end

  c.default_cassette_options = { :serialize_with => :syck }
  c.cassette_library_dir = 'cassettes'
  <configuration>
end

VCR.use_cassette('example') do
  puts response_body_for(:get, "http://localhost:#{$server_8888.port}/")
end

VCR.use_cassette('example') do
  puts response_body_for(:get, "http://localhost:#{$server.port}/")
end

puts response_body_for(:get, "http://localhost:#{$server.port}/")
puts response_body_for(:get, "http://localhost:#{$server_8888.port}/")
```

*When* I run `ruby ignore_request.rb`

*Then* it should fail with an error like:

```
An HTTP request has been made that VCR does not know how to handle:
```

*And* the output should contain:

```
Port 8888 Response 1
Port 7777 Response 1
Port 7777 Response 2
```

*And* the file "cassettes/example.yml" should contain "Port 8888"

*And* the file "cassettes/example.yml" should not contain "Port 7777".

### Examples

| configuration          | http\_lib              |
| ---------------------- | ---------------------- |
| c.hook\_into :webmock  | net/http               |
| c.hook\_into :typhoeus | typhoeus               |
| c.hook\_into :faraday  | faraday (w/ net\_http) |

## ignored host requests are not recorded and are always allowed

*Given* a file named "ignore\_hosts.rb" with:

```
include_http_adapter_for("<http_lib>")
require 'sinatra_app.rb'

require 'vcr'

VCR.configure do |c|
  c.ignore_hosts '127.0.0.1', 'localhost'
  c.cassette_library_dir = 'cassettes'
  <configuration>
end

VCR.use_cassette('example') do
  puts response_body_for(:get, "http://localhost:#{$server.port}/")
end

puts response_body_for(:get, "http://localhost:#{$server.port}/")
```

*When* I run `ruby ignore_hosts.rb`

*Then* it should pass with:

```
Port 7777 Response 1
Port 7777 Response 2
```

*And* the file "cassettes/example.yml" should not exist.

### Examples

| configuration          | http\_lib              |
| ---------------------- | ---------------------- |
| c.hook\_into :webmock  | net/http               |
| c.hook\_into :typhoeus | typhoeus               |
| c.hook\_into :excon    | excon                  |
| c.hook\_into :faraday  | faraday (w/ net\_http) |

## unignored host requests are recorded again

*Given* a file named "unignore\_hosts.rb" with:

```
include_http_adapter_for("<http_lib>")
require 'sinatra_app.rb'

require 'vcr'

VCR.configure do |c|
  c.ignore_hosts '127.0.0.1', 'localhost'
  c.cassette_library_dir = 'cassettes'
  <configuration>
end

VCR.use_cassette('example') do
  puts response_body_for(:get, "http://localhost:#{$server.port}/")
end

VCR.configure do |c|
  c.unignore_hosts '127.0.0.1', 'localhost'
end

VCR.use_cassette('example') do
  puts response_body_for(:get, "http://localhost:#{$server.port}/")
end
```

*When* I run `ruby unignore_hosts.rb`

*Then* it should pass with:

```
Port 7777 Response 1
Port 7777 Response 2
```

*And* the file "cassettes/example.yml" should not contain "Response 1"

*And* the file "cassettes/example.yml" should contain "Response 2".

### Examples

| configuration          | http\_lib              |
| ---------------------- | ---------------------- |
| c.hook\_into :webmock  | net/http               |
| c.hook\_into :typhoeus | typhoeus               |
| c.hook\_into :excon    | excon                  |
| c.hook\_into :faraday  | faraday (w/ net\_http) |

## unignored host requests are not allowed without a cassette

*Given* a file named "unignore\_hosts\_without\_cassette.rb" with:

```
include_http_adapter_for("<http_lib>")
require 'sinatra_app.rb'

require 'vcr'

VCR.configure do |c|
  c.ignore_hosts '127.0.0.1', 'localhost'
  c.cassette_library_dir = 'cassettes'
  <configuration>
end

puts response_body_for(:get, "http://localhost:#{$server.port}/")

VCR.configure do |c|
  c.unignore_hosts '127.0.0.1', 'localhost'
end

puts response_body_for(:get, "http://localhost:#{$server.port}/")
```

*When* I run `ruby unignore_hosts_without_cassette.rb`

*Then* it should fail with "An HTTP request has been made that VCR does not know how to handle"

*And* the output should contain "Response 1"

*And* the output should not contain "Response 2"

*And* the file "cassettes/example.yml" should not exist.

### Examples

| configuration          | http\_lib              |
| ---------------------- | ---------------------- |
| c.hook\_into :webmock  | net/http               |
| c.hook\_into :typhoeus | typhoeus               |
| c.hook\_into :excon    | excon                  |
| c.hook\_into :faraday  | faraday (w/ net\_http) |

## localhost requests are not treated differently by default

*Given* a file named "localhost\_not\_ignored.rb" with:

```
include_http_adapter_for("<http_lib>")
require 'sinatra_app.rb'

require 'vcr'

VCR.configure do |c|
  c.cassette_library_dir = 'cassettes'
  c.default_cassette_options = { :serialize_with => :syck }
  <configuration>
end

VCR.use_cassette('localhost') do
  response_body_for(:get, "http://localhost:#{$server.port}/")
end

response_body_for(:get, "http://localhost:#{$server.port}/")
```

*When* I run `ruby localhost_not_ignored.rb`

*Then* it should fail with "An HTTP request has been made that VCR does not know how to handle"

*And* the file "cassettes/localhost.yml" should contain "Port 7777 Response 1".

### Examples

| configuration          | http\_lib              |
| ---------------------- | ---------------------- |
| c.hook\_into :webmock  | net/http               |
| c.hook\_into :typhoeus | typhoeus               |
| c.hook\_into :excon    | excon                  |
| c.hook\_into :faraday  | faraday (w/ net\_http) |

## localhost requests are allowed and not recorded when ignore\_localhost = true

*Given* a file named "ignore\_localhost\_true.rb" with:

```
include_http_adapter_for("<http_lib>")
require 'sinatra_app.rb'

require 'vcr'

VCR.configure do |c|
  c.ignore_localhost = true
  c.cassette_library_dir = 'cassettes'
  <configuration>
end

VCR.use_cassette('localhost') do
  puts response_body_for(:get, "http://localhost:#{$server.port}/")
end

puts response_body_for(:get, "http://localhost:#{$server.port}/")
```

*When* I run `ruby ignore_localhost_true.rb`

*Then* it should pass with:

```
Port 7777 Response 1
Port 7777 Response 2
```

*And* the file "cassettes/localhost.yml" should not exist.

### Examples

| configuration          | http\_lib              |
| ---------------------- | ---------------------- |
| c.hook\_into :webmock  | net/http               |
| c.hook\_into :typhoeus | typhoeus               |
| c.hook\_into :excon    | excon                  |
| c.hook\_into :faraday  | faraday (w/ net\_http) |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://andrewmcodes.gitbook.io/vcr/configuration/ignore_request.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
