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

cdesai1987
try
{


string url5 = "https://api.kite.trade/orders/regular";
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",
tradingsymbol="ACC",
exchange="NSE" ,
transaction_type="BUY" ,
order_type="MARKET" ,
quantity="1" ,
product="MIS" ,
validity="DAY"
});
streamWriter.Write(loginjson1);
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)R1.GetResponse();



}
catch (Exception ex)
{
Response.Write(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("https://api.kite.trade/orders/regular");
    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
    os.Close();
    System.Net.WebResponse resp = req.GetResponse();


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

    Response.Write(sr.ReadToEnd().Trim());
  • 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.