How to edit response body returned by VCR gem?

1,880 views
Skip to first unread message

cleyton...@gmail.com

unread,
Feb 20, 2014, 4:50:56 PM2/20/14
to vcr-...@googlegroups.com

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

Myron Marston

unread,
Feb 20, 2014, 4:54:19 PM2/20/14
to vcr-...@googlegroups.com, cleyton...@gmail.com
The binary blob is how psych serializes strings with the ASCII-8BIT encoding:


There's another thread where we discussed this at length and there are a number of solutions mentioned there:


HTH,
Myron 

cleyton...@gmail.com

unread,
Feb 20, 2014, 4:58:54 PM2/20/14
to vcr-...@googlegroups.com, cleyton...@gmail.com
Oh.

I've tried one solution where you can serialize as JSON, but the entire file is serialized as JSON.

So, it all stays in one line, making impossible to edit it.

I'd like to not serialize only the response and it return as POSTman, for example, returns, as a well formatted JSON

Myron Marston

unread,
Feb 20, 2014, 5:09:00 PM2/20/14
to vcr-...@googlegroups.com, cleyton...@gmail.com
VCR doesn't have any sort of mixed-mode serializer built in, but writing your own serializer is pretty easy:


If your problem with serializing the entire thing as json is simply the fact that it's all on one line, you could easily write a serializer that uses `JSON.pretty_generate`:


...which would nicely format the JSON for you.

Myron

cleyton...@gmail.com

unread,
Feb 20, 2014, 5:16:45 PM2/20/14
to vcr-...@googlegroups.com, cleyton...@gmail.com
Thanks for your idea!

I googled for it and I found this nice code: https://github.com/mislav/movieapp/blob/master/spec/support/vcr.rb

With some minor modifications, it did was I intending to do.

Thanks!
Reply all
Reply to author
Forward
0 new messages