Click here to Skip to main content
15,029,010 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:
  "orderType": "MARKET",
  "session": "NORMAL",
  "duration": "DAY",
  "orderStrategyType": "SINGLE",
  "orderLegCollection": [
      "instruction": "Buy",
      "quantity": 15,
      "instrument": {
        "symbol": "XYZ",
        "assetType": "EQUITY"


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

What I have tried:

This is the code I have tried:
Dim url As String
Dim strBuyURLBody As String

url = ""
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:
"{" & vbLf & "  ""error"" : ""A validation error occurred while processing the request.""" & vbLf & "}"
Updated 1-Oct-20 0:56am

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.

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