HttpClient not supporting PostAsJsonAsync method C#
Yes, you need to add a reference to
System.Net.Http.Formatting.dll
This can be found in the extensions assemblies area.
A good way of achieving this is by adding the NuGet package Microsoft.AspNet.WebApi.Client
to your project.
HttpClient not supporting PostAsJsonAsync method & dll in C#
The error is pretty clear. Here you can see what all the overloads of that method are. You probably want to pass the uri and the payload object, something like:
HttpResponseMessage response = client.PostAsJsonAsync("/api/webapi", [your object instance here]);
Currently, you are passing a single String, hence the error.
Also, the using statement that needs correction:
using System.Net.Http.Formatting;
Compiler can't find PostAsJsonAsync method
If you gonna use PostAsJsonAsync
method then you need to use following NuGet Package.
However, if you're using DNX that it won't work because it does not support DNX (yet?).
It that case you can do the following:
var response = await client.PostAsync("api/values", new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(item);, Encoding.UTF8,"application/json"));
That should help.
C# HttpClient.PostAsJsonAsync Failing with Internal Server Error
Finally figured it out. I had to set the content type on the HttpClient object itself. Added these 2 lines in the using block and it worked!
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
PostAsJsonAsync() not working in .Net Core 3.1 when update project from 2.2 to 3.1 (Missing Assembly) or How to consume API POST request in .NET Core
This works for me.
Dot net core 3.x doesn't have any assembly for PostAsJsonAsync().
And Dot Net Core 2. not supported by Microsoft. So we should not use anything that not in 3.x. should not add assembly from 2.x.
The possible alternative is PostAsync();
HttpResponseMessage resNews =
await client.PostAsync("News/GetNews", new StringContent(JsonConvert.SerializeObject(Object),
Encoding.UTF8, "application/json"));
If pass only string:
HttpResponseMessage resNews =
await client.PostAsync("News/GetNews", new StringContent("string"));
Related Topics
How to Convert an Iso8601 Timespan to a C# Timespan
How to Get a Uri of the Image Stored in the Resources
Overloading Getter and Setter Causes a Stack Overflow in C#
How to Initialize a C# Dictionary with Values
How to Save Console.Writeline Output to Text File
How to Get the Word Under the Cursor in Windows
Generically Flatten JSON Using C#
Graphics.Drawstring VS Textrenderer.Drawtextwhich Can Deliver Better Quality
Should You Implement Idisposable.Dispose() So That It Never Throws
Deserialize Nested JSON into C# Objects
SQL Where Clause Matching Values with Trailing Spaces
How to Bind Crystal Report to Manually Created Dataset
Singleton by Jon Skeet Clarification
What's Better: Dataset or Datareader