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"));
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.
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.
ASP.NET Core 3.1 - PostAsync/PostAsJsonAsync method in Integration Test always returns Bad Request
You are missing the FromBody
attribute from you action parameter. When you are sending json data to a controller that will be part of the request body. You can tell to the controller how to bind the incoming data, in your case from the body. So you code should look like:
public async Task<IActionResult> Register([FromBody]UserRegistrationRequest request)
{
…
}
You could read more about bindings in the official documentation.
HttpClient PostAsJsonAsync behaving different in .NET Core and Classic .NET
The underlying problem appears to be that the Classic .NET WebAPI fails to process chunked requests in it current newest version (v5.2.3). I'm going to assume that's a bug and as a work-around downgrade to v5.0 for the time being.
Special thanks to @Developer for helpful input.
-S
Get Values in .Net Core Api send via PostAsJsonAsync in .Net Core Application
you send an object and your backend excepts deconstructed values. your API should look like below;
[HttpPost]
public JsonResult PostNewStamptime([FromBody] LoginModel model)
{...}
And LoginModel should contain key and login properties like Below
public class LoginModel {
public string Key {get;set;}
public bool Login {get;set;}
}
Also, you can avoid using a model by altering endpoints statusVerb to Get and send the parameters in query strings. That's when you can reach them via [FromQuery]. But this is semantically wrong.
Send HTTP POST message in ASP.NET Core using HttpClient PostAsJsonAsync
You should add reference to "Microsoft.AspNet.WebApi.Client" package (read this article for samples).
Without any additional extension, you may use standard PostAsync
method:
client.PostAsync(uri, new StringContent(jsonInString, Encoding.UTF8, "application/json"));
where jsonInString
value you can get by calling JsonConvert.SerializeObject(<your object>);
net Core 3.1 Object null in WebApi method after PostAsJsonAsync
You need to construct your http client like this:
_client = new HttpClient { BaseAddress = new Uri("your http://my base url goes here"),
Timeout = new TimeSpan(0, 0, 0, 0, -1) };
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));//add json header
//_client.DefaultRequestHeaders.Add("Bearer", "some token goes here");
and you need to call your method like this:
var postTask = await _client.PostAsJsonAsync("AgregarNegocio", model);
make sure you call "await" on it because it is async.
NOTES:
Notice that I added MediaTypeWithQualityHeaderValue to indicate that it is json.
Also using Route usually is not a good idea... It is better to use HttPost("MyRoute") because it combined the ControllerName + Route. But it is up to you.
Related Topics
How to Get the Currently Loggedin Windows Account from an ASP.NET Page
How to Cast a List into a Type Which Inherits from List<T>
How to Remove Empty Lines from a Formatted String
How to Set a Cookie on Httpclient'S Httprequestmessage
How to Submit Disabled Input in ASP.NET MVC
Multidimensional Array from a Txt File
Use Latest Version of Internet Explorer in the Webbrowser Control
How to Convert English Digits to Arabic Digits
Check If Combobox Value Is Empty
Moq - Non-Overridable Members May Not Be Used in Setup/Verification Expressions
Why Am I Getting Error Cs0246: the Type or Namespace Name Could Not Be Found
How to Directly Print a Report Without Going Through Crystal Reports Viewer
How to Delete All Files in an Azure File Storage Folder
How to Upload a Excel File to SQL Database Table Using C# Windows Form Application
Asp.Net Core API Post Parameter Is Always Null
Microsoft Azure: How to Create Sub Directory in a Blob Container