Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My web api is as bellow

C#
public class MultibuysController : ApiController
    
        private const int DEFAULT_TIMEOUT = 2000;
        readonly IMultibuyService _multibuyService;

        public MultibuysController()
        {
            _multibuyService = new MultibuyService();
        }

        public MultibuysController(IMultibuyService multibuyService)
        {
            _multibuyService = multibuyService;
        }

        // GET api/multibuys/1234560101
        public Discounts Get(string id, int timeout = DEFAULT_TIMEOUT)
        {
            return _multibuyService.GetMultibuyItemDiscount(id, timeout);
        }

        // POST api/multibuys
        public Discounts Post(Bag bag, int timeout = DEFAULT_TIMEOUT)
        {
            return _multibuyService.GetMultibuyItemDiscount(bag, timeout);
        }
    }


from my client website i call the api get method
as bellow
C#
public Discounts AllItems(string id)
        {
            List<Product> list = new List<Product>();
            Bag _discountedBag = new Bag();
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:56835/");       
            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("api/multibuys"));
        
            HttpResponseMessage response = client.GetAsync("api/multibuys/Get?id=?" + id).Result;
          
            if (response.IsSuccessStatusCode)
            {
                 return response.Content.ReadAsAsync<Discounts>().Result;
                 
            }
          

            return new Discounts();
        }
now i wanted to create a method to call the post method from my client web
but now sure how to use the PostAsyn could some one help me to on this plz
what i want is
C#
public Discounts PostABag(Bag bag)
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:56835/"); 
            HttpResponseMessage response = client.PostAsync("api/multibuys/Post?bag=?"

	//need help to finish off this function 
        }

appreciate all your help
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900