Hi developers I am consuming web api first time .
This is my json object to authenticate a user . how can I pass this asp.net to call web api for getting authenticate token please help me
POST /v2.0/tokens HTTP/1.1
User-Agent: curl/7.21.0 (x86_64-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.15 libssh2/1.2.6
Host: identity.api.rackspacecloud.com
Accept: application/json
Content-Type: application/json
Content-Length: 54
{
"auth":
{
"RAX-KSKEY:apiKeyCredentials":
{
"username": (1)"jsmith",
"apiKey": (2)"aaaaa-bbbbb-ccccc-12345678"
}
}
}
My requirement is that I want to Post this json object to server and get the response from server but I am getting exception of bad request 400. what is the cause of this? I asked to google about this many times and found lots of solution and tried all those but still I am getting same error
I am using this code in asp.net
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://identity.api.rackspacecloud.com/v2.0/tokens");
request.Method = "POST";
string postData = "{ \"auth\":{\"RAX-KSKEY:apiKeyCredentials\":{\"username\":\"jsmith\" ,\"apikey\":\"aaaaa-bbbbb-ccccc-12345678\" }} }";
var byteArray = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(postData));
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
request.Accept = "application/json";
request.ContentType= "application/json";
request.ContentLength = data.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(data, 0, data.Length);
dataStream.Close();
Getting exception in this line
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
}
please give me some solution .......