Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClient object method missing

Tags:

c#

I am separating some code out of a website and after copying the code behind for the particular page in question, I'm getting an error on the PostAsJsonAsync() line of code:

HttpResponseMessage response = await client.PostAsJsonAsync("api/...", user);

which is in this using statement (added headers as well)

        using System;
        using System.Net.Http;
        using System.Net.Http.Headers;
        using System.Net.Mail;
        using System.Threading.Tasks;
        //...

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

            HttpResponseMessage response = await client.PostAsJsonAsync("api/...", user);
            if (response.IsSuccessStatusCode)
            {
                const string result = "Thank you for your submission.";
                return result;
            }
            //...
        }

The error I get says

 Error  4   'System.Net.Http.HttpClient'
 does not contain a definition for 'PostAsJsonAsync' and no extension
 method 'PostAsJsonAsync' accepting a first argument of type 'System.Net.Http.HttpClient'
 could be found (are you missing a using directive or an assembly reference?)

even though it works in the former project and was copied straight over from that project in its entirety. Did I forget to add something?

I appreciate any help on the matter.

like image 930
Rex_C Avatar asked Apr 28 '15 15:04

Rex_C


People also ask

Which method is not part of HttpClient?

HttpClient' does not contain a definition for 'PostAsJsonAsync' and No extension method 'PostAsJsonAsync' accepting a first argument of type 'System.

What is PostAsJsonAsync?

PostAsJsonAsync<T>(HttpClient, Uri, T) Sends a POST request as an asynchronous operation to the specified Uri with the given value serialized as JSON. PostAsJsonAsync<T>(HttpClient, Uri, T, CancellationToken) Sends a POST request as an asynchronous operation to the specified Uri with the given value serialized as JSON.

Does HttpClient support https?

C# Only http and https schemes are allowed - HttpClient - Microsoft Q&A.


2 Answers

You will have to add following dependency,

System.Net.Http.Formatting.dll

It should be there in extensions -> assembly.

or

You can add Microsoft.AspNet.WebApi.Client nuget package

like image 139
Abhishek Avatar answered Oct 09 '22 20:10

Abhishek


I wrote my own extension method as I believe that method is only for older .NET api, and used Newtonsoft JSON serializer:

// Extension method to post a JSON
public static async Task<HttpResponseMessage> PostAsJsonAsync(this HttpClient client, string addr, object obj)
{
    var response = await client.PostAsync(addr, new StringContent(
            Newtonsoft.Json.JsonConvert.SerializeObject(obj),
            Encoding.UTF8, "application/json"));

    return response;
}
like image 43
noelicus Avatar answered Oct 09 '22 19:10

noelicus