7

I'm getting a The remote server returned an error: (400) Bad Request error when trying to run my code. Any help would be appreciated. Thanks.

    // Open request and set post data
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("myurl.com/restservice/Login");
    request.Method = "POST";
    request.ContentType = "application/json; charset:utf-8";
    string postData = "{ \"username\": \"testname\" },{ \"password\": \"testpass\" }";

    // Write postData to request url
    using (Stream s = request.GetRequestStream())
    {
        using (StreamWriter sw = new StreamWriter(s))
            sw.Write(postData);
    }

    // Get response and read it
    using (Stream s = request.GetResponse().GetResponseStream()) // error happens here
    {
        using (StreamReader sr = new StreamReader(s))
        {
            var jsonData = sr.ReadToEnd();
        }
    }

JSON EDIT

Changed to:

{ \"username\": \"jeff\", \"password\": \"welcome\" }

But still not working.

EDIT

This is what I found that works:

       // Open request and set post data
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("myurl.com/restservice/Login");
    request.Method = "POST";
    request.ContentType = "application/json";
    string postData = "{ \"username\": \"testname\", \"password\": \"testpass\" }";

    // Set postData to byte type and set content length
    byte[] postBytes = System.Text.UTF8Encoding.UTF8.GetBytes(postData);
    request.ContentLength = postBytes.Length;

    // Write postBytes to request stream
    Stream s = request.GetRequestStream();
    s.Write(postBytes, 0, postBytes.Length);
    s.Close();

    // Get the reponse
    WebResponse response = request.GetResponse();

    // Status for debugging
    string ResponseStatus = (((HttpWebResponse)response).StatusDescription);

    // Get the content from server and read it from the stream
    s = response.GetResponseStream();
    StreamReader reader = new StreamReader(s);
    string responseFromServer = reader.ReadToEnd();

    // Clean up and close
    reader.Close();
    s.Close();
    response.Close();
Trey Copeland
  • 3,387
  • 7
  • 29
  • 46
  • Why are you mucking around with Streams? Use a [`WebClient`](http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx) instead! – qJake Apr 12 '12 at 20:40
  • 1
    WebClient is a good idea. But also, note that a 400 request usually denotes the server not understanding your request. A bad payload is the likely culprit, especially since you seem to have incorrect JSON. – yamen Apr 12 '12 at 20:43

3 Answers3

5

can you try string postData = "[{ \"username\": \"testname\" },{ \"password\": \"testpass\" }]";

That way you are sending an array of 2 objects

Edit: Also maybe what you really want to send is just an object with 2 properties, then it would be string postData = "{ \"username\": \"testname\", \"password\": \"testpass\" }"

jorgehmv
  • 3,633
  • 3
  • 25
  • 39
0

It looks as though it may be coming from the JSON you are posting as it is invalid, see below what you are sending but in a valid form:

{
    "username": "testname",
    "password": "testpass"
}
  • As I said in my other comment, many REST services return a HTTP 400 if the data is invalid, such as Viamenete and Palletways. I have just today been working with a HTTP based web service that does this exact same thing. –  Apr 12 '12 at 20:43
0

postData is not a valid Json object

{ "username": "testname" },{ "password": "testpass" }

Try to use a Json parser like Json.Net, JavaScriptSerializer or DataContractJsonSerializer instead of forming it manually

L.B
  • 114,136
  • 19
  • 178
  • 224