I have developed a Web API. I have tested all methods with Postman and all methods are working ok. So I developed a small windows app to code the calls before putting in a mobile application.
All Get methods are working ok but I am having a problem when I called a PUT method. I am passing the parameters with HttpContent but the response I got from my app is 404, Not Found. As I said if I use Postman it works ok. here is the calling method:
private string UpdateStudent()
{
string retval = string.Empty;
const string chave = "minhachave";
string name = "MeuNome";
string mob = "0987654321";
string mail = "[email protected]";
string psw = "MinhaSenha";
var webapiuri = new Uri("https://localhost:44310");
HttpClient client = new HttpClient();
List<string> param = new List<string>();
param.Add(name);
param.Add(mob);
param.Add(mail);
param.Add(psw);
param.Add(chave);
try
{
string jsonString = JsonConvert.SerializeObject(param);
var requestUrl = new Uri(webapiuri + "/api/Students/UpdateStd");
using (HttpContent httpContent = new StringContent(jsonString))
{
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage response = client.PutAsync(requestUrl, httpContent).Result;
retval = response.ToString();
}
}
catch(Exception err)
{
string error = err.Message;
retval = error;
}
return retval;
}
And this is my API method:
//Method to update registered student. Only update mobile, email and password
//PUT: api/Students/UpdateStd
[HttpPut]
public string UpdateStd(string name, string mobile, string email, string password, string chave)
{
string valchave = "minhachave";
string retval = string.Empty;
bool chvval = valchave.Equals(chave);
try
{
if (chvval == true)
{
tblRegister retstud = dtclmob.tblRegisters.Single(s => s.Name == name);
if (retstud != null)
{
retstud.Email = email;
retstud.Mobile = mobile;
retstud.Password = password;
dtclmob.SubmitChanges();
retval = "Studentd info updated.";
}
else
{
retval = "Student not found.";
}
}
else
{
retval = "WRONG KEY";
}
}
catch(Exception err)
{
retval = err.Message;
}
return retval;
}
Any suggestions will be really appreciated.
After some digging and reading I found my way. First I change the API method I use the [FromBody] and use the Student model as parameters. In my call method I used the same model where I added the new values to be updated. That way I didn't have to worry about all the parametes to be passed. Thanks.