Search code examples
c#jsonpostinteractive-brokers

How to make a JSON post using C# from the operations of Ninja Trader?


I need to perform a JSON CSharp Post from the Ninja Trader to one for Api C# of the Interactive Broker, using following:

{
"Instrument": "SIE",
"Side": "BUY",
"Volume": "200",
"Price": "100",
"Type": "LMT",
"Accounts": ["DU1107110", "DU1107170", "DU1107180", "DU1107190"],
"Currency": "EUR",
"Exchange": "SMART",
"SecType": "STK",
"PrimaryExch": "",
"Gives you": ""
}

Solution

  • You need to create a c# object using data first. then you need to serialize that object eg var json = JsonConvert.SerializeObject(data);

    Now all you need to do is pass the string to the post method.

    var stringContent = new StringContent(json, UnicodeEncoding.Utf8, "application/json");

    var client = new HttpClient(); var response = await client.PostAsync(uri, stringContent);