Click here to Skip to main content
15,039,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to do a post request with RESTSHARP using VB.NET and I can't get it to work.
The POST BODY JSON string must be:
JavaScript
{
  "orderType": "MARKET",
  "session": "NORMAL",
  "duration": "DAY",
  "orderStrategyType": "SINGLE",
  "orderLegCollection": [
    {
      "instruction": "Buy",
      "quantity": 15,
      "instrument": {
        "symbol": "XYZ",
        "assetType": "EQUITY"
      }
    }
  ]
}

PLEASE HELP....

If you know a better way I would work with you...
THANKS!!

What I have tried:

This is the vb.net code I have tried:
VB.NET
Dim url As String
Dim strBuyURLBody As String

url = "https://api.tdameritrade.com/v1/accounts/myaccountnumber/orders"
Dim client = New RestClient(url)
Dim postRequest = New RestRequest(Method.POST)
postRequest.AddHeader("cache-control", "no-cache")
postRequest.AddHeader("content-type", "application/json")
postRequest.AddHeader("Authorization", "Bearer myvalidaccesscode")
postRequest.AddJsonBody(New With {Key .session = "AM", Key .duration = "FILL_OR_KILL", Key .orderType = "MARKET", Key .orderStrategyType = "Single", .orderLegCollection = New With {Key .instruction = "BUY", Key .quantity = 1, .instrument = New With {Key .symbol = "MSFT", Key .assetType = "EQUITY"}}})
Dim response1 As IRestResponse = client.Execute(postRequest)
strJSON = response1.Content

The error message I get is:
VB.NET
"{" & vbLf & "  ""error"" : ""A validation error occurred while processing the request.""" & vbLf & "}"
Posted
Updated 1-Oct-20 0:56am
v2

1 solution

Your JSON body content doesn't match the required structure. The orderLegCollection needs to be a collection of objects, but you've only passed a single object.

Try:
VB.NET
postRequest.AddJsonBody(New With {Key .session = "AM", Key .duration = "FILL_OR_KILL", Key .orderType = "MARKET", Key .orderStrategyType = "Single", .orderLegCollection = New Object() { New With {Key .instruction = "BUY", Key .quantity = 1, .instrument = New With {Key .symbol = "MSFT", Key .assetType = "EQUITY"}}}})
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900