<p>I am trying to build a C# application working on JSON-RPC with Bitcoin-qt (server mode).
I am using Bitsharp, though I don't think this should matter at all.</p>

<p>First off the InvokeMethod:</p>

<pre><code>        public JObject InvokeMethod(string a_sMethod, params object[] a_params)
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
        webRequest.Credentials = Credentials;

        webRequest.ContentType = "application/json-rpc";
        webRequest.Method = "POST";

        JObject joe = new JObject();
        joe["jsonrpc"] = "1.0";
        joe["id"] = "1";
        joe["method"] = a_sMethod;

        if (a_params != null)
        {
            if (a_params.Length &gt; 0)
            {
                JArray props = new JArray();
                foreach (var p in a_params)
                {
                    props.Add(p);
                }
                joe.Add(new JProperty("params", props));
            }
        }

        string s = JsonConvert.SerializeObject(joe);
        // serialize json for the request
        byte[] byteArray = Encoding.UTF8.GetBytes(s);
        webRequest.ContentLength = byteArray.Length;

        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"D:\WriteLines2.txt", true))
        {
            //file.WriteLine(s);
        }
        using (Stream dataStream = webRequest.GetRequestStream())
        {
            dataStream.Write(byteArray, 0, byteArray.Length);
        }

        using (WebResponse webResponse = webRequest.GetResponse())
        {
            using (Stream str = webResponse.GetResponseStream())
            {
                using (StreamReader sr = new StreamReader(str))
                {
                    return JsonConvert.DeserializeObject&lt;JObject&gt;(sr.ReadToEnd());
                }
            }
        }
    }
</code></pre>

<p>Secondly, this is the object I am building:</p>

<pre><code>JContainer jArray = new JArray();

        JArray jArrayTx = new JArray();

        foreach (var ObjectTx in inputs.result)
        {
            JObject jObjectTx = new JObject();
            jObjectTx.Add("txid", ObjectTx.txid);
            jObjectTx.Add("vout", ObjectTx.vout);
            jArrayTx.Add(jObjectTx);
        }

        jArray.Add(jArrayTx);

        JObject jObjectAddress = new JObject();

        foreach (var ObjectAddress in designatedAddresses)
        {
            jObjectAddress.Add(ObjectAddress.Key, ObjectAddress.Value);
        }
        if (returnAddress != null)
        {
            jObjectAddress.Add(returnAddress, totalUnspent - totalOutput - 0.001);
        }
        jArray.Add(jObjectAddress);
</code></pre>

<p>Then I do this:</p>

<pre><code>bc.InvokeMethod("createrawtransaction", jArray);
</code></pre>

<p>I am trying to pass an object containing 2 arrays.
1. the array of txid's + vout's
2. the array of addresses I want to send to + the amounts.</p>

<p>This for some reason doesn't work, and i can't seem to put my finger on the issue.</p>

<p>Thanks for your help!</p>

<ul>
<li>EDIT -</li>
</ul>

<p>Tried posting my JSON-RPC to file and then running the command directly in the bitcoin-qt command line.</p>

<p>This is the JSON-RPC object which doesn't work</p>

<pre><code>{"jsonrpc":"1.0","id":"1","method":"createrawtransaction","params":[[[{"txid":"a89fd1381833a800942812a3981fe9910e364dc4a5fa91332354f8fc8ce02a1f","vout":0},{"txid":"b45394e27c2beb6b53dcefb79646eebcd9fd406bd9810f4b4138e00adadff637","vout":1},{"txid":"cae05651bb8f02cb0848bc984537290c375956d5fdfb3ae7e9d4280196f41765","vout":1},{"txid":"cbf93b0d8bbcadd85b3702653dfe2643d63cdd7ebf5b66f8a3d4c271b2a55492","vout":1}],{"mmhmMNfBiZZ37g1tgg2t8DDbNoEdqKVxAL":1.4,"n2JSa2L2KY4Eq32jEpBmKZrZTdxgCaNVtR":5.8735192599999992}]]}
</code></pre>

<p>or in object view:</p>

<pre><code>    {
  "jsonrpc": "1.0",
  "id": "1",
  "method": "createrawtransaction",
  "params": [
    [
      [
        {
          "txid": "a89fd1381833a800942812a3981fe9910e364dc4a5fa91332354f8fc8ce02a1f",
          "vout": 0
        },
        {
          "txid": "b45394e27c2beb6b53dcefb79646eebcd9fd406bd9810f4b4138e00adadff637",
          "vout": 1
        },
        {
          "txid": "cae05651bb8f02cb0848bc984537290c375956d5fdfb3ae7e9d4280196f41765",
          "vout": 1
        },
        {
          "txid": "cbf93b0d8bbcadd85b3702653dfe2643d63cdd7ebf5b66f8a3d4c271b2a55492",
          "vout": 1
        }
      ],
      {
        "mmhmMNfBiZZ37g1tgg2t8DDbNoEdqKVxAL": 1.4,
        "n2JSa2L2KY4Eq32jEpBmKZrZTdxgCaNVtR": 5.8735192599999992
      }
    ]
  ]
}
</code></pre>

<p>This does work:</p>

<pre><code>createrawtransaction '[{"txid":"a89fd1381833a800942812a3981fe9910e364dc4a5fa91332354f8fc8ce02a1f","vout":0},{"txid":"b45394e27c2beb6b53dcefb79646eebcd9fd406bd9810f4b4138e00adadff637","vout":1},{"txid":"cae05651bb8f02cb0848bc984537290c375956d5fdfb3ae7e9d4280196f41765","vout":1},{"txid":"cbf93b0d8bbcadd85b3702653dfe2643d63cdd7ebf5b66f8a3d4c271b2a55492","vout":1}]' '{"mmhmMNfBiZZ37g1tgg2t8DDbNoEdqKVxAL":1.4,"n2JSa2L2KY4Eq32jEpBmKZrZTdxgCaNVtR":5.8735192599999992}'
</code></pre>

<p>What was needed for it to work is to remove the excess <strong>[</strong> and to wrap the tx's in <strong>' '</strong> and add a white space between the tx's and the spending addresses.</p>

<p>** This works in the bitcoin-qt console, though i am unable to get createrawtransaction to work on json-rpc</p>

CC BY-SA 3.0
2013-10-30T16:05:26.580Z
What error is the RPC API reporting?  I've worked a little bit with raw transactions.  The RPC engine, though not extremely explicit, does output which portion of the transaction is broken.
RLH
CC BY-SA 3.0
2013-10-30T17:31:50.393Z
The RPC-API returns:
"The remote server returned an error: <500> Internal server error"
And the server log shows:
"ThreadRPCServer method=createrawtransaction
connection timeout"
Chen
CC BY-SA 3.0
2013-10-30T19:36:43.847Z
Try saving the full rpc command to a file and then execute the command directly against bitcoind.
RLH
CC BY-SA 3.0
2013-10-31T20:01:56.970Z
Added some more info to the main post, still unable to get this to work :\
Chen
CC BY-SA 3.0
2014-01-05T18:49:42.303Z
@George , didn't find any way to get this to work. Just went to process one transaction at a time
Chen
<transactions><json-rpc><c#><raw-transaction>
C# - JSON-RPC - How to build the json object before serialization (createrawtransaction)
Chen
# C# - JSON-RPC - How to build the json object before serialization (createrawtransaction)

(Asked by: username_0 on 2013-10-30)

I am trying to build a C# application working on JSON-RPC with Bitcoin-qt (server mode).
I am using Bitsharp, though I don't think this should matter at all.

First off the InvokeMethod:

```
        public JObject InvokeMethod(string a_sMethod, params object[] a_params)
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
        webRequest.Credentials = Credentials;

        webRequest.ContentType = "application/json-rpc";
        webRequest.Method = "POST";

        JObject joe = new JObject();
        joe["jsonrpc"] = "1.0";
        joe["id"] = "1";
        joe["method"] = a_sMethod;

        if (a_params != null)
        {
            if (a_params.Length > 0)
            {
                JArray props = new JArray();
                foreach (var p in a_params)
                {
                    props.Add(p);
                }
                joe.Add(new JProperty("params", props));
            }
        }

        string s = JsonConvert.SerializeObject(joe);
        // serialize json for the request
        byte[] byteArray = Encoding.UTF8.GetBytes(s);
        webRequest.ContentLength = byteArray.Length;

        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"D:\WriteLines2.txt", true))
        {
            //file.WriteLine(s);
        }
        using (Stream dataStream = webRequest.GetRequestStream())
        {
            dataStream.Write(byteArray, 0, byteArray.Length);
        }

        using (WebResponse webResponse = webRequest.GetResponse())
        {
            using (Stream str = webResponse.GetResponseStream())
            {
                using (StreamReader sr = new StreamReader(str))
                {
                    return JsonConvert.DeserializeObject<JObject>(sr.ReadToEnd());
                }
            }
        }
    }

```

Secondly, this is the object I am building:

```
JContainer jArray = new JArray();

        JArray jArrayTx = new JArray();

        foreach (var ObjectTx in inputs.result)
        {
            JObject jObjectTx = new JObject();
            jObjectTx.Add("txid", ObjectTx.txid);
            jObjectTx.Add("vout", ObjectTx.vout);
            jArrayTx.Add(jObjectTx);
        }

        jArray.Add(jArrayTx);

        JObject jObjectAddress = new JObject();

        foreach (var ObjectAddress in designatedAddresses)
        {
            jObjectAddress.Add(ObjectAddress.Key, ObjectAddress.Value);
        }
        if (returnAddress != null)
        {
            jObjectAddress.Add(returnAddress, totalUnspent - totalOutput - 0.001);
        }
        jArray.Add(jObjectAddress);

```

Then I do this:

```
bc.InvokeMethod("createrawtransaction", jArray);

```

I am trying to pass an object containing 2 arrays.
1. the array of txid's + vout's
2. the array of addresses I want to send to + the amounts.

This for some reason doesn't work, and i can't seem to put my finger on the issue.

Thanks for your help!

* EDIT -

Tried posting my JSON-RPC to file and then running the command directly in the bitcoin-qt command line.

This is the JSON-RPC object which doesn't work

```
{"jsonrpc":"1.0","id":"1","method":"createrawtransaction","params":[[[{"txid":"a89fd1381833a800942812a3981fe9910e364dc4a5fa91332354f8fc8ce02a1f","vout":0},{"txid":"b45394e27c2beb6b53dcefb79646eebcd9fd406bd9810f4b4138e00adadff637","vout":1},{"txid":"cae05651bb8f02cb0848bc984537290c375956d5fdfb3ae7e9d4280196f41765","vout":1},{"txid":"cbf93b0d8bbcadd85b3702653dfe2643d63cdd7ebf5b66f8a3d4c271b2a55492","vout":1}],{"mmhmMNfBiZZ37g1tgg2t8DDbNoEdqKVxAL":1.4,"n2JSa2L2KY4Eq32jEpBmKZrZTdxgCaNVtR":5.8735192599999992}]]}

```

or in object view:

```
    {
  "jsonrpc": "1.0",
  "id": "1",
  "method": "createrawtransaction",
  "params": [
    [
      [
        {
          "txid": "a89fd1381833a800942812a3981fe9910e364dc4a5fa91332354f8fc8ce02a1f",
          "vout": 0
        },
        {
          "txid": "b45394e27c2beb6b53dcefb79646eebcd9fd406bd9810f4b4138e00adadff637",
          "vout": 1
        },
        {
          "txid": "cae05651bb8f02cb0848bc984537290c375956d5fdfb3ae7e9d4280196f41765",
          "vout": 1
        },
        {
          "txid": "cbf93b0d8bbcadd85b3702653dfe2643d63cdd7ebf5b66f8a3d4c271b2a55492",
          "vout": 1
        }
      ],
      {
        "mmhmMNfBiZZ37g1tgg2t8DDbNoEdqKVxAL": 1.4,
        "n2JSa2L2KY4Eq32jEpBmKZrZTdxgCaNVtR": 5.8735192599999992
      }
    ]
  ]
}

```

This does work:

```
createrawtransaction '[{"txid":"a89fd1381833a800942812a3981fe9910e364dc4a5fa91332354f8fc8ce02a1f","vout":0},{"txid":"b45394e27c2beb6b53dcefb79646eebcd9fd406bd9810f4b4138e00adadff637","vout":1},{"txid":"cae05651bb8f02cb0848bc984537290c375956d5fdfb3ae7e9d4280196f41765","vout":1},{"txid":"cbf93b0d8bbcadd85b3702653dfe2643d63cdd7ebf5b66f8a3d4c271b2a55492","vout":1}]' '{"mmhmMNfBiZZ37g1tgg2t8DDbNoEdqKVxAL":1.4,"n2JSa2L2KY4Eq32jEpBmKZrZTdxgCaNVtR":5.8735192599999992}'

```

What was needed for it to work is to remove the excess **[** and to wrap the tx's in **' '** and add a white space between the tx's and the spending addresses.

\*\* This works in the bitcoin-qt console, though i am unable to get createrawtransaction to work on json-rpc

**Tags:** transactions, json-rpc, c#, raw-transaction

> **Comments:**
> **username_1**: What error is the RPC API reporting?  I've worked a little bit with raw transactions.  The RPC engine, though not extremely explicit, does output which portion of the transaction is broken.  
> **username_0**: The RPC-API returns: "The remote server returned an error: <500> Internal server error" And the server log shows: "ThreadRPCServer method=createrawtransaction connection timeout"  
> **username_1**: Try saving the full rpc command to a file and then execute the command directly against bitcoind.  
> **username_0**: Added some more info to the main post, still unable to get this to work :\  
> **username_0**: @George , didn't find any way to get this to work. Just went to process one transaction at a time  

---

## 0 answers

