18

I'm using VCR to record responses from other systems that i'm integrating with.

But, this response get a huge JSON an VCR is saving it in a binary format:

body:
  encoding: ASCII-8BIT
  string: !binary |-
    eyJsaXN0IjpbXSwiZmFjZXRzIjpbeyJuYW1lIjoiU2FsZXNDaGFubmVsTmFt
    ZSIsInR5cGUiOi...

Is there a way where I can save ONLY the response body as JSON?

I want to do this to edit the returned JSON in order to make other scenarios for my tests,

Thanks

Cleyton
  • 2,338
  • 6
  • 23
  • 39

3 Answers3

30

From the google forum link that Cleyton supplied (I would leave a comment if I had the rep), the following change in spec_helper.rb works for me:

VCR.configure do |c|
  c.before_record do |i|
    i.response.body.force_encoding('UTF-8')
  end
end
polpetti
  • 787
  • 7
  • 21
5

Use decode_compressed_response in your configuration.

VCR.configure do |c|
  c.cassette_library_dir = 'cassettes'
  c.hook_into :webmock
  c.default_cassette_options = { :decode_compressed_response => true } 
end
TlmaK0
  • 3,578
  • 2
  • 31
  • 51
  • 1
    I am using VCR 5.0.0 with this option, but still end up with binary code in my recordings. My options are set to `{ serialize_with: :yaml, record: :once, allow_playback_repeats: true, decode_compressed_response: true }` – Tom Rossi Jun 24 '19 at 15:34
4

Looking in VCR google forum, I was told to do my own serializer in order to get a pretty json return.

So, i've found this code. With some minor modifications, it solved my problem, formatting the response body to not be encoded as binary.

Cleyton
  • 2,338
  • 6
  • 23
  • 39
  • To be clear, the only modification I had to make was to remove the filter_sensitive_data parts, and it worked great. Thanks! – d3vkit Nov 14 '14 at 00:43