I am new and create winform application. In which I have to use the API for a simple CRUD operation. My client shared an API with me and asked me to send the data in JSON form.
API: http://blabla.com/blabla/api/login-valida
KEY: "HelloWorld"
Value: {"email": " user@gmail.com ", "password": "123456", "time": "2015-09-22 10:15:20"}
Answer: Login_id
How can I convert the data to JSON, call the API using the POST method and get a response?
EDIT
Somewhere on stackoverflow I found this solution
public static void POST(string url, string jsonContent)
{
url="blabla.com/api/blala" + url;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseURL);
request.Method = "POST";
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
Byte[] byteArray = encoding.GetBytes(jsonContent);
request.ContentLength = byteArray.Length;
request.ContentType = @"application/json";
using (Stream dataStream = request.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
}
long length = 0;
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
length = response.ContentLength;
}
}
catch
{
throw;
}
}
private void btnLogin_Click(object sender, EventArgs e)
{
CallAPI.POST("login-validate", "{ \"email\":" + txtUserName.Text + " ,\"password\":" + txtPassword.Text + ",\"time\": " + DateTime.Now.ToString("yyyy-MM-dd h:mm tt") + "}");
}
I got an exception that says: "The remote server returned an error: (404) Not Found."