Post parameter is always null
Since you have only one parameter, you could try decorating it with the [FromBody]
attribute, or change the method to accept a DTO with value as a property, as I suggested here: MVC4 RC WebApi parameter binding
UPDATE: The official ASP.NET site was updated today with an excellent explanation: https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/sending-html-form-data-part-1
In a nutshell, when sending a single simple type in the body, send just the value prefixed with an equal sign (=), e.g. body:
=test
Asp.net Core Post parameter is always null
I just had to correct the double quotes in your POST request and it worked. Try this:
{"Ean":"1122u88991","Name":"Post test","Description":"Post test desc"}
See screenshot below.
ASP.NET Core API POST parameter is always null
The problem is that the Content-Type
is application/json
, whereas the request payload is actually text/plain
. That will cause a 415 Unsupported Media Type HTTP error.
You have at least two options to align then Content-Type
and the actual content.
Use application/json
Keep the Content-Type
as application/json
and make sure the request payload is valid JSON. For instance, make your request payload this:
{
"cookie": "=sec_session_id=[redacted]; _ga=[redacted]; AWSELB=[redacted]"
}
Then the action signature needs to accept an object with the same shape as the JSON object.
public class CookieWrapper
{
public string Cookie { get; set; }
}
Instead of the CookieWrapper
class, or you can accept dynamic, or a Dictionary<string, string>
and access it like cookie["cookie"]
in the endpoint
public IActionResult GetRankings([FromBody] CookieWrapper cookie)
public IActionResult GetRankings([FromBody] dynamic cookie)
public IActionResult GetRankings([FromBody] Dictionary<string, string> cookie)
Use text/plain
The other alternative is to change your Content-Type
to text/plain
and to add a plain text input formatter to your project. To do that, create the following class.
public class TextPlainInputFormatter : TextInputFormatter
{
public TextPlainInputFormatter()
{
SupportedMediaTypes.Add("text/plain");
SupportedEncodings.Add(UTF8EncodingWithoutBOM);
SupportedEncodings.Add(UTF16EncodingLittleEndian);
}
protected override bool CanReadType(Type type)
{
return type == typeof(string);
}
public override async Task<InputFormatterResult> ReadRequestBodyAsync(
InputFormatterContext context,
Encoding encoding)
{
string data = null;
using (var streamReader = context.ReaderFactory(
context.HttpContext.Request.Body,
encoding))
{
data = await streamReader.ReadToEndAsync();
}
return InputFormatterResult.Success(data);
}
}
And configure Mvc to use it.
services.AddMvc(options =>
{
options.InputFormatters.Add(new TextPlainInputFormatter());
});
See also
https://github.com/aspnet/Mvc/issues/5137
ASP.Net Core 3.1 - Post parameter is always NULL in Web API
You have defined FromQuery
attribute for your parameter. In fact when you post x-www-form-urlencoded
form there's a certain content-type specified that tells model binding system all query parameters are actually form fields. So you have to either define FromForm
attribute for xml
parameter,
[HttpPost("verify_string")]
public ActionResult<ICollection<Certificate>> VerifyXmlString([FromForm] string xml)
either pass it as a query parameter using Params tab in postman.
asp.net webapi 2 post parameter is always null
Do you always send the same parameters? If so, could you create a static object instead of using a dynamic one? Something like an EventRequest
that you pass in instead?
public class EventRequest
{
public int CheckinCount { get; set; }
public int EventId { get; set; }
public int OrderNo { get; set; }
}
Your Post
action then becomes:
public IHttpActionResult Post(EventRequest request) {
// do something with request
return Ok();
}
Why are POST params always null in .NET Core API
You can't get two separate parameters from body, cause it is going to bind email
and password
as properties of the parameter, not exact parameters. You need to create an object, which defines properties with those names, and use that object in the parameter
class Login
{
public string Email { get; set; }
public string Password { get; set; }
}
In the action method parameter
([FromBody]Login model)
ASP.NET Core API POST parameter is always null from Firefox
I went through same issue once, and It took almost a day to figure out the reason behind it,
do check your date pickers and its values, and make sure it is not null and its format is also correct. Because firefox is a bit strict in this matter and a litter change in datepicker makes it null.
hope it helps you.
ASP.NET WebApi HttpPost parameter always null
Add the FromBody attribute to the parameter
[HttpPost]
public void Post([FromBody]DataUrl dataUrlIN)
{
}
Check out the Parameter Binding Documentation for more information
Why post parameter is always null asp.net core web API from Reactjs?
when using [FromBody]
, the JSON-formatted request body is handled by the JsonInputFormatter
and System.Text.Json
(asp.net core 3.0) , so that just correctly-formatted DateTime value for JSON will work :
Document link : https://learn.microsoft.com/en-us/dotnet/standard/datetime/system-text-json-support
So if your string sent from client is format like YYYY-MM-DD HH:mm:ss
, that won't work . You can change the format on client side , or you can just send via query string(make sure request is formed without space) or send as part of route value .
Related Topics
Swap Two Variables Without Using a Temporary Variable
How to Read and Write from the Serial Port
What Does the Tilde Before a Function Name Mean in C#
How to Sort Strings Alphabetically While Accounting for Value When a String Is Numeric
When Should Taskcompletionsource<T> Be Used
C#: Printing All Properties of an Object
Are Static Class Instances Unique to a Request or a Server in ASP.NET
Get the Titles of All Open Windows
Onclientclick and Onclick Is Not Working at the Same Time
Method Call Using Ternary Operator
Regex Match Multiple Times in String
Releasing a Unplugged Virtual Serial Port
Getting Path Relative to the Current Working Directory
How to Programmatically Fill in a Form and 'Post' a Web Page
Ide's for C# Development on Linux
How to Check If a String Contains Any of Some Strings
Collection Was Modified; Enumeration Operation May Not Execute in Arraylist