I'm working on integrating with an API that accepts files and data in the same POST request and would like to use VCR to mock the request/response cycle but am running into trouble posting a file via VCR. As VCR initializes a new request there appears to be a check that we're trying to send a string in vcr/structs.rb L64-66 but it appears that HTTPClient is trying to send this via a streamed connection since it includes a file. A minimal example demonstrating the failure can be found in this gist.
Thanks for reporting this. Sounds like an integration bug with WebMock. @bblimke -- maybe WebMock is missing logic to handle multipart posts with HTTPClient?
Thanks for the quick response, if I get some time later this week I'll look into it but since I'm not familiar with either VCR or WebMock's codebase it may take a while.
Greetings gents. Not sure what specifics I can add to this but we are hitting the same issue. Where would you recommend starting to look to implement handling this behavior? I can see the check in VCR for the string body, but I don't understand enough about the depenecies from HTTPClient for instance to translate this. Commenting out the check lets this work for us but there are side-effects I am sure I am not aware of.
WebMock indeed doesn't support multipart bodies. The tricky part here is that on each request, part boundary strings are different so it's difficult to figure out whether it's the same request or not. Simple body dump string comparison won't work here.
It looks like HTTPClient#create_boundary gets used to build that boundary string. Perhaps we could have webmock monkey patch over that method with something that was constant but related to the request (SHA1 of the url and file?). That doesn't, however, deal with the case of a user building up their own body including a multipart boundary.
It's more relevant to WebMock ability to stub these kind of requests. VCR has own request matching mechanism,
and since these request bodies are non deterministic, body would have to be excluded from matching criteria
Can you have a try with latest WebMock 1.12.3 please?
Thanks @bblimke, that seemed to work well. The bodies are correctly recorded in the VCR file and VCR isn't complaining about non-string bodies.