API Not Found when called from Windows app but works on Postman

52 Views Asked by At

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.

2

There are 2 best solutions below

1
MMuniz On

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.

0
Chander Kumar On

According to your API, you have to send the map/dictionary object as parameters to call your API, but you are only send the List, that is the reason your are getting API not found error. I refector your UpdateStudent function and make it little better, please try it .

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();

    try
    {
        // Create an object to hold your parameters
        var parameters = new
        {
            name = name,
            mobile = mob,
            email = mail,
            password = psw,
            chave = chave
        };

        // Serialize the object to JSON
        string jsonString = JsonConvert.SerializeObject(parameters);

        // Create the request URL
        var requestUrl = new Uri(webapiuri + "/api/Students/UpdateStd");

        // Create the HttpContent for the request
        using (HttpContent httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json"))
        {
            // Send the PUT request
            HttpResponseMessage response = client.PutAsync(requestUrl, httpContent).Result;

            // Check if the request was successful (status code 200 OK)
            if (response.IsSuccessStatusCode)
            {
                retval = "Student updated successfully!";
            }
            else
            {
                retval = $"Error: {response.StatusCode} - {response.ReasonPhrase}";
            }
        }
    }
    catch (Exception err)
    {
        string error = err.Message;
        retval = error;
    }
    return retval;
}