fvukovic
fvukovic

Reputation: 719

Cant add parameters to post api call in xamarin forms

i'm new at Xamarin, trying to build a simple app. i'm trying to make a simple post call where i send one parameter. this is my code:

    private async Task fetchWwarrents(){
        var request = new HttpRequestMessage(HttpMethod.Post,"http://perductor.hr/aplikacija/rest/radninalozi.php");  
        var keyValues = new List<KeyValuePair<string, string>>{
            new KeyValuePair<string, string>("driver","1")
    };
        request.Content = new FormUrlEncodedContent(keyValues);
        var client = new HttpClient();
        var responseMessage = await client.SendAsync(request);
        var jwt = await responseMessage.Content.ReadAsStringAsync(); 
    } 

it works, i mean i get the data from the api, but the driver parameter is not recognized. I test the api with postman and when i post the driver parameter it is working..I don't know where the problem is, can somebody help me?

Upvotes: 0

Views: 734

Answers (1)

Pratius Dubey
Pratius Dubey

Reputation: 673

You are sending json request is wrong format, just you need to modify the Httpclient request.

private async Task fetchWwarrents()
{
        try
            {
                using (var client = new HttpClient())
                {
                    HttpContent content=null;
                    var json = JsonConvert.SerializeObject(RquestString);
                    content = new StringContent(json, Encoding.UTF8, "application/json");
                    using (var response = await client.PostAsync("http://perductor.hr/aplikacija/rest/radninalozi.php", content))
                    {
                        var responseStr = await response.Content.ReadAsStringAsync();


                        if (!string.IsEmpty(responseStr))
                        {
                            //Parse the json string to object as per requirment
                        }
                        else
                        {
                            //Message
                        }

                    }


                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(@"ERROR {0}", ex.Message);

            } 
}

Upvotes: 2

Related Questions