Md. Abdul Alim
Md. Abdul Alim

Reputation: 689

Calling Web API from Desktop Client

Working on a WPF project. A post request send with content type 'application/x-www-form-urlencoded' which were working nice for web client. But when request send from WPF project then its conten receive null from API.

        public static string urlEncoded = "application/x-www-form-urlencoded";
        public static async Task<HttpResponseMessage> SendRequest(HttpMethod method, string endPoint, dynamic content = null)
        {
            HttpResponseMessage response = null;
            using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage(method, endPoint))
                {
                    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(urlEncoded));
                    if (content != null)
                    {
                        string c;
                        if (content is string)
                            c = content;
                        else
                            c = JsonConvert.SerializeObject(content);
                        request.Content = new StringContent(c, Encoding.UTF8, urlEncoded);
                    }

                    response = await client.SendAsync(request).ConfigureAwait(false);
                }
            }
            return response;

        }

Anyone can help me?

Upvotes: 0

Views: 64

Answers (1)

Md. Abdul Alim
Md. Abdul Alim

Reputation: 689

Finally I get a solution from this. Just changing content

var keyValues = new List<KeyValuePair<string, string>>()
{
   // Here Adding body
}
request.Content = new FormUrlEncodedContent(keyValues);

Then its working.

Upvotes: 1

Related Questions