Logging request data for rest-client POST body
Closed this issue · 2 comments
When I run the example from the readme:
RestClient.post 'http://example.com/resource', {param1: 'one', nested: {param2: 'two'}
I get the following log:
D [2018-01-03T15:53:21.839364] #4acdbe6b: [httplog] Connecting: example.com:80
D [2018-01-03T15:53:22.002725] #4acdbe6b: [httplog] Sending: POST http://example.com:80/resource
D [2018-01-03T15:53:22.003110] #4acdbe6b: [httplog] Data:
D [2018-01-03T15:53:22.334041] #4acdbe6b: [httplog] Status: 404
D [2018-01-03T15:53:22.334462] #4acdbe6b: [httplog] Benchmark: 0.33069 seconds
D [2018-01-03T15:53:22.334764] #4acdbe6b: [httplog] Response:
D [2018-01-03T15:53:22.334764] #4acdbe6b: <?xml version="1.0" encoding="iso-8859-1"?>
D [2018-01-03T15:53:22.334764] #4acdbe6b: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
D [2018-01-03T15:53:22.334764] #4acdbe6b: "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
D [2018-01-03T15:53:22.334764] #4acdbe6b: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
D [2018-01-03T15:53:22.334764] #4acdbe6b: <head>
D [2018-01-03T15:53:22.334764] #4acdbe6b: <title>404 - Not Found</title>
D [2018-01-03T15:53:22.334764] #4acdbe6b: </head>
D [2018-01-03T15:53:22.334764] #4acdbe6b: <body>
D [2018-01-03T15:53:22.334764] #4acdbe6b: <h1>404 - Not Found</h1>
D [2018-01-03T15:53:22.334764] #4acdbe6b: </body>
D [2018-01-03T15:53:22.334764] #4acdbe6b: </html>
Note that the data is empty. Tested in rest-client 2.0.2, httplog 1.0.0.
Thanks for reporting this, I'll take a look.
Ok, tricky one. Turns out the RestClient gem sends POST requests as a stream if the payload responds to :read
(see here), and it wraps pretty much any type of data into such a payload - StringIO
for plain strings and hashes (see here).
The problem is that in order to print that data into the log, I'd have to read the stream, which in the case of StringIO
would then require me to rewind it, but the stream is an instance variable of the Payload
class, and it doesn't expose a rewind
or seek
method to reset it. And I'm very loath to start introducing adapter-specific logic into my already somewhat brittle monkey patches.
TL;DR there's no easy fix. I'll add a note to that effect to the README.