Below code is not working ."The remote server returned an error: (400) Bad Request "" Please Help


string url5 = "";
var R1 = (HttpWebRequest)WebRequest.Create(url5);
R1.ContentType = "application/json; charset=UTF-8";
R1.Method = "POST";
using (var streamWriter = new StreamWriter(R1.GetRequestStream()))
string loginjson1 = new JavaScriptSerializer().Serialize(new
api_key = "XXXXX",
access_token = "XXXXX",
exchange="NSE" ,
transaction_type="BUY" ,
order_type="MARKET" ,
quantity="1" ,
product="MIS" ,
var httpResponse = (HttpWebResponse)R1.GetResponse();

catch (Exception ex)
  • Kailash
    You are sending a JSON payload, but the API only accepts standard form encoded POST parameters.
  • cdesai1987
    cdesai1987 edited September 2016
    Even the below code which is non Json code is not working... Please Help

    System.Net.WebRequest req = System.Net.WebRequest.Create("");
    req.ContentType = "application/x-www-form-urlencoded";
    req.Method = "POST";

    byte[] bytes = System.Text.Encoding.ASCII.GetBytes("api_key=xxx&access_token=" + accesstoken + "&tradingsymbol=ACC&exchange=NSE&transaction_type=BUY&order_type=Market&quantity=1&product=MIS&validity=DAY");
    req.ContentLength = bytes.Length;
    System.IO.Stream os = req.GetRequestStream();
    os.Write(bytes, 0, bytes.Length); //Push it out there
    System.Net.WebResponse resp = req.GetResponse();

    System.IO.StreamReader sr =
    new System.IO.StreamReader(resp.GetResponseStream());

  • Kailash
    What's the error message that you are getting?
  • cdesai1987
    cdesai1987 edited September 2016
    The remote server returned an error: (400) Bad Request
  • cdesai1987
    I am sending it from local https url. I am getting the access token from here but cannot execute the trades
  • Vivek
    Vivek edited September 2016
    @cdesai1987 Please check for error message in response. You might get a meaningful error message which will help you to debug it. Generally 400 means you are sending invalid data or not sending required params.
  • cdesai1987
    It works now thanks
This discussion has been closed.