0

This is my webapi post request

    [HttpPost]
    [Route("Create/{id}")]
    public async Task<IActionResult> CreateContact(Guid id, string email, string fullName)
    {
        // code removed for brevity
    }

How do I post contact object over to the webapi? This is what I have in the client.

   using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("https://localhost:123");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var contact = new Contact() { Id = 12345, Email = "test@gmail.com", FullName = "John" };
            HttpResponseMessage response = await client.PostAsJsonAsync($"/api/Contact/Create/{contact.Id}", contact);

            if (response.IsSuccessStatusCode)
            {
                
            }
            else
            {
         
            }
        }
Steve
  • 2,963
  • 15
  • 61
  • 133
  • 1
    var contact = new Contact() { id = "12345", email = "test@gmail.com", fullName = "John" }; – MD. RAKIB HASAN Jan 21 '21 at 10:34
  • 1
    You can append the parameters values in URL and send them as query string – Chetan Jan 21 '21 at 10:44
  • https://stackoverflow.com/questions/43158250/how-to-post-using-httpclient-content-type-application-x-www-form-urlencoded – Chetan Jan 21 '21 at 10:46
  • Does this answer your question? [How to POST using HTTPclient content type = application/x-www-form-urlencoded](https://stackoverflow.com/questions/43158250/how-to-post-using-httpclient-content-type-application-x-www-form-urlencoded) – Chetan Jan 21 '21 at 10:48
  • @ChetanRanpariya, I found the solution but not the solution of your post – Steve Jan 21 '21 at 10:54

2 Answers2

1

Not sure if this is the best but it works. Add more parameter at the Route attribute

[HttpPost]
[Route("Create/{id}/{email}/{fullName}")]
public async Task<IActionResult> CreateContact(Guid id, string email, string fullName)
{
    // code removed for brevity
}

and then at the httpclient

 HttpResponseMessage response = await client.PostAsJsonAsync($"/api/Contact/Create/{contact.Id}/{contact.Email}/{contact.FullName}", contact);
Steve
  • 2,963
  • 15
  • 61
  • 133
0

Hi there Use [FromBody] in your WPI. The you can post the Contact as an object

    [HttpPost]
    [Route("Create/{contact}")]
    public async Task<IActionResult> CreateContact([FromBody]Contact contact)
    {
        // code removed for brevity
    }
   using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("https://localhost:123");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var contact = new Contact() { Id = 12345, Email = "test@gmail.com", FullName = "John" };
            var contactJson = JsonConvert.SerializeObject(contact);
            var stringContent = new StringContent(contactJson , UnicodeEncoding.UTF8, "application/json");

            HttpResponseMessage response = await client.PostAsync($"/api/Contact/Create/", stringContent);

            if (response.IsSuccessStatusCode)
            {
                
            }
            else
            {
         
            }
        }
Theo Koekemoer
  • 228
  • 2
  • 5