Sunday 8 November 2020

Passing Data to Server - POST using QuaryString - How to Handle this In Xamarin Forms

Sometime we get in situation that we have to pass data in URL String and method is Created as POST. So in Xamarin How we send this,Check below scenario.

Ex:

http://serveraddress.in/v1/test/cart/add?userID=39964949d-d5-44ff-967169-4ec6b725e121fg&productID=0c0d65a239-e352-4a6e-98b5-0a55a0327aa4&size=L&quantity=1

Then above method is created as POST and We are Passing into Parameter so how we handle this in Xamarin Forms.

Methods:

public async Task AddProductToCart(string userID, string productID, string size, string quantity)
{
        var url = Configuration.Instance.MyDomainAPIUri;

        var data = "?userID=" + userID + "&productID=" + productID + "&size=" + size + "&quantity=" + quantity;

        var stringContent = new StringContent(data, UnicodeEncoding.UTF8, "application/json");


                using (var client = new HttpClient())
                {
                        await AddHeaders(client, false);

                        using (var response = await client.PostAsync(url+data, stringContent))
                                    {
                                        if (response.StatusCode == System.Net.HttpStatusCode.OK)
                                                        {
                                                                try
                                                                   {
                                                                    var properties = await response.Content.ReadAsStringAsync();
                                                                    return properties;
                                                                    }
                                                                catch (Exception ex)
                                                                    {
                                                                    return null;
                                                                    }
                                                            }
                                           else
                                                        {

                                                            return null;
                                                        }

                                           } // INNER USING
                        } // OUTER USING
}

No comments:

Post a Comment

All About .NET MAUI

  What’s .NET MAUI? .NET MAUI (.NET Multi-platform App UI) is a framework for building modern, multi-platform, natively compiled iOS, Androi...

Ads2