From 22b6af14000cb451983297cfab3d0e2061b6a69d Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Wed, 18 Jun 2014 10:16:09 -0500 Subject: [PATCH] Fix unmarshaling of HTTP POST responses. If connecting to a bitcoin RPC server as an HTTP POST client, full response objects rather than just the raw result bytes were being passed to the specific result unmarshalers, causing unmarshal errors for the incorrect JSON types. To fix this, first unmarshal the response body into a rawResponse, and pass only the raw result bytes (or an error) to the specific handlers. This was reported by nskelsey on IRC. ok @davecgh --- infrastructure.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/infrastructure.go b/infrastructure.go index b3bf7bdf..bb8b60d0 100644 --- a/infrastructure.go +++ b/infrastructure.go @@ -271,7 +271,7 @@ func (r rawResponse) result() (result []byte, err error) { // handleMessage is the main handler for incoming notifications and responses. func (c *Client) handleMessage(msg []byte) { // Attempt to unmarshal the message as either a notifiation or response. - in := inMessage{} + var in inMessage err := json.Unmarshal(msg, &in) if err != nil { log.Warnf("Remote server sent invalid message: %v", err) @@ -618,12 +618,19 @@ func (c *Client) handleSendPostMessage(details *sendPostDetails) { } // Read the raw bytes and close the response. - resp, err := btcjson.GetRaw(httpResponse.Body) + respBytes, err := btcjson.GetRaw(httpResponse.Body) if err != nil { details.responseChan <- &response{result: nil, err: err} return } - details.responseChan <- &response{result: resp, err: nil} + var resp rawResponse + err = json.Unmarshal(respBytes, &resp) + if err != nil { + details.responseChan <- &response{result: nil, err: err} + return + } + res, err := resp.result() + details.responseChan <- &response{result: res, err: err} } // sendPostHandler handles all outgoing messages when the client is running