VCR
v-6.0.0
v-6.0.0
  • Introduction
  • Upgrade
  • Changelog
  • About These Examples
  • License
  • Contributing
  • Cassettes
    • Cassette Format
    • Naming
    • Error for HTTP Request Made When No Cassette Is in Use
    • Dynamic ERB Cassettes
    • Automatic Re-Recording
    • Exclusive Cassette
    • Update Content Length Header
    • Decode Compressed Response
    • Allow Unused HTTP Interactions
    • Freezing Time
  • Record modes
    • Once
    • New Episodes
    • None
    • All
    • Record on Error
  • Configuration
    • Cassette Library Dir
    • Hook Into
    • Default Cassette Options
    • Ignore Request
    • Filter Sensitive Data
    • Allow HTTP Connections When No Cassette
    • Debug Logging
    • Preserve Exact Body Bytes
    • URI Parser
    • Query Parser
  • Hooks
    • Before Record Hook
    • Before Playback Hook
    • Before HTTP Request Hook
    • After HTTP Request Hook
    • Around HTTP Request Hook
  • Request matching
    • Introduction
    • Matching on Method
    • Matching on URI
    • Matching on Host
    • Matching on Path
    • Matching on Query String
    • Matching on Body
    • Matching on Headers
    • Identical Requests Are Replayed in Sequence
    • Register and Use a Custom Matcher
    • URI Without Param(s)
    • Playback Repeats
    • Matching on Body as JSON
  • Test frameworks
    • Usage With Test::Unit
    • Usage With RSpec Metadata
    • Usage With Cucumber
  • Middleware
    • Rack
    • Faraday Middleware
  • HTTP Libraries
    • Net::HTTP
    • EM HTTP Request
Powered by GitBook
On this page
  • log all requests and responses using after_http_request hook
  • Examples

Was this helpful?

  1. Hooks

After HTTP Request Hook

The after_http_request hook gets called with each request and response just after a request has completed. It can be used for many things:

  • globally logging requests and responses

  • ejecting the current cassette (i.e. if you inserted it in a before_http_request hook)

    You can also pass one or more "filters" to after_http_request, to make the hook only be called for some requests. Any object that responds to #to_proc can be a filter. Here are some simple examples:

  • :real? -- only real requests

  • :stubbed? -- only stubbed requests

  • :ignored? -- only ignored requests

  • :recordable? -- only requests that are being recorded

  • lambda { |req| URI(req.uri).host == 'amazon.com' } -- only requests to amazon.com.

log all requests and responses using after_http_request hook

Given a file named "after_http_request.rb" with:

include_http_adapter_for("<http_lib>")

$server = start_sinatra_app do
  get('/foo') { "Hello World (foo)" }
  get('/bar') { "Hello World (bar)" }
end

require 'vcr'

VCR.configure do |c|
  <configuration>
  c.cassette_library_dir = 'cassettes'
  c.ignore_localhost = true
  c.after_http_request(:ignored?, lambda { |req| req.uri =~ /foo/ }) do |request, response|
    uri = request.uri.sub(/:\d+/, ":7777")
    puts "Response for #{request.method} #{uri}: #{response.body}"
  end
end

make_http_request(:get, "http://localhost:#{$server.port}/foo")
make_http_request(:get, "http://localhost:#{$server.port}/bar")

When I run ruby after_http_request.rb

But the output should not contain "bar".

Examples

configuration

http_lib

c.hook_into :webmock

net/http

c.hook_into :webmock

httpclient

c.hook_into :webmock

curb

c.hook_into :typhoeus

typhoeus

c.hook_into :excon

excon

c.hook_into :faraday

faraday (w/ net_http)

PreviousBefore HTTP Request HookNextAround HTTP Request Hook

Last updated 4 years ago

Was this helpful?

Then the output should contain "Response for get : Hello World (foo)"

http://localhost:7777/foo