Trouble posting CREATERAWTRANSACTION to Bitcoin Core via JSON-RPC
I think the problem is your params
array is getting double-serialized, so the server doesn't know how to interpret the request. I do realize that your JSON looks the same as the example, so I could very well be wrong here; I'm definitely not an expert on using Bitcoin Core's API. However, I did look at a the source code for a third-party library which is supposed to be compatible with Bitcoin Core, and it does not appear to double-serialize the parameters for the createrawtransation
request. This leads me to believe that the double-serialized parameters are the problem.
To fix, try the following:
Change the method signature for your existing
RequestServer
method from this:public static string RequestServer(string methodName, List<string> parameters)
to this:
public static string RequestServer(string methodName, List<JToken> parameters)
Create a new overload of the
RequestServer
method using the old signature which calls the existing one you just changed. This will allow your other methods which already work (e.g.sendtoaddress
andlistunspent
) to keep working with no changes.public static string RequestServer(string methodName, List<string> parameters){ return RequestServer(methodName, parameters.Select(p => new JValue(p)).ToList<JToken>());}
Lastly, change the code in your
button1_Click
method so that it does not serializejArray
andjArray2
, but instead passes them in aList<JToken>
toRequestServer
. In other words, change this code:string strFrom = JsonConvert.SerializeObject(jArray);string strTo = JsonConvert.SerializeObject(jArray2);data = JObject.Parse(RequestServer("createrawtransaction", new List<string>() { strFrom, strTo }));
to this:
data = JObject.Parse(RequestServer("createrawtransaction", new List<JToken>() { jArray, jArray2 }));
With these changes, the RPC JSON for createrawtransaction
should end up looking like this:
{"jsonrpc":"1.0","id":"1","method":"createrawtransaction","params":[[{"txid":"1a43a1f27c5837d5319a45217aa948a4d39c1d89faf497ce59de5bd570a64a26","vout":1}],[{"2NAZpRsvj9BstxxCDkKoe1FVjmPPxdmvqKj":0.01}]]}
Notice that the extra quotes and backslashes in the params
array are gone. Compare to what you had before:
{"jsonrpc":"1.0","id":"1","method":"createrawtransaction","params":["[{\"txid\":\"1a43a1f27c5837d5319a45217aa948a4d39c1d89faf497ce59de5bd570a64a26\",\"vout\":1}]","[{\"2NAZpRsvj9BstxxCDkKoe1FVjmPPxdmvqKj\":0.01}]"]}