Hubert Solecki
Hubert Solecki

Reputation: 2761

Post JSON Object using httpClient windows Phone 8.1

I'm trying to post a json Object to a web api project from a windows phone app but I'm still getting 404 error. For the post method, I'm using that code:

Mail mailToCheck = new Mail();
try
             {
                 mailToCheck.MailProfil = TxtBox_mail.Text.ToString();
                 string json = JsonConvert.SerializeObject(mailToCheck);
                 var httpClient = new System.Net.Http.HttpClient(new HttpClientHandler());
                 System.Net.Http.HttpResponseMessage response = await httpClient.PostAsync(new Uri("http://uri/api/Profil/CheckMail"), new StringContent(json));
                 var responseString = await response.Content.ReadAsStringAsync();
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.HResult.ToString());
             }

The method CheckMail on my conctroller:

[HttpPost]
    [Route("api/Profil/CheckMail")]
    public IHttpActionResult CheckMail([FromBody]Mail MailProfil)
    {
        if (MailProfil.MailProfil != null)
        {
            try
            {
                bool exists = Librairie.Profils.mailExists(MailProfil.MailProfil);
                return Ok(exists);
            }
            catch(Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        else
        {
            return BadRequest();
        }
    }

The Mail object is exactly the same in the app as in the web api project. Does someone can tell me what I'm doing wrong here ?

Upvotes: 0

Views: 2134

Answers (1)

kiewic
kiewic

Reputation: 16390

Check some samples of HttpClient.PostAsync() here: https://monkeyweekend.wordpress.com/2014/10/23/how-to-send-text-json-or-files-using-httpclient-postasync/

Upvotes: 3

Related Questions