Receiving the remote server returned an error: (400) Bad Request while fetching Margin


Trying to retrieving margin using below link using (HttpWebRequest c#)

[{ "exchange": "NFO", "tradingsymbol": "TECHM22JAN2000CE", "transaction_type": "SELL", "variety": "regular", "product": "NRML", "order_type": "MARKET", "quantity":600, "price": 0, "trigger_price": 0 }]

[{ "exchange": "NFO", "tradingsymbol": "AMBUJACEM22JAN375PE", "transaction_type": "SELL", "variety": "regular", "product": "NRML", "order_type": "MARKET", "quantity":1500, "price": 0, "trigger_price": 0 }]

[{ "exchange": "NFO", "tradingsymbol": "NIFTY22JAN16850PE", "transaction_type": "SELL", "variety": "regular", "product": "NRML", "order_type": "MARKET", "quantity":50, "price": 0, "trigger_price": 0 }]

Receiving the remote server returned an error: (400) Bad Request.

  • sujith
    Can you give us the exact json response? Can you also mention the version of the library you are using?
  • arjunender
    not using any kite library instead using this

    Below is the c# code:
    dynamic json;
    var url = "";
    var httpRequest = (HttpWebRequest)WebRequest.Create(url);
    httpRequest.Method = "POST";
    httpRequest.Headers["Authorization"] = "token " + API + ":" + MyAccessToken;
    httpRequest.ContentType = "application/x-www-form-urlencoded";
    var data = "[{\"exchange\": \"NFO\",\"tradingsymbol\": \"AMBUJACEM22JAN370PE\",\"transaction_type\": \"SELL\",\"variety\": \"regular\",\"product\": \"NRML\",\"order_type\": \"MARKET\",\"quantity\": 1500,\"price\": 0,\"trigger_price\": 0}]";
    using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
    var httpResponse = (HttpWebResponse)httpRequest.GetResponse(); // at this line getting this error System.Net.WebException: 'The remote server returned an error: (400) Bad Request.'
    string result;
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    result = streamReader.ReadToEnd();
    json = JsonConvert.DeserializeObject(result);

    The same was working before
  • rakeshr
    httpRequest.ContentType = "application/x-www-form-urlencoded";
    You are sending wrong content-type header, it should be application/json, refer complete curl request params here.
  • arjunender
    thanks rakesh its working

    one more thing how to add consider_positions=false in kite using below code

    OrderMarginParams param = new OrderMarginParams();
    param.Exchange = "NFO";
    param.TradingSymbol = "AMBUJACEM22JAN370PE"
    param.TransactionType = "SELL";
    param.Quantity = 1500;
    param.Price = 0m;
    param.OrderType = "MARKET";
    param.Product = "NRML";
    List margins = kite.GetOrderMargins(new List() { param });
  • sujith
    You can refer to the code here.
  • arjunender
    thanks sujith
This discussion has been closed.