Microsoft.AspNetCore.Http.BadHttpRequestException: Failed to read
parameter "RegisterRequest registration" from the request body as
JSON. —> System.Text.Json.JsonException: JSON deserialization for
type ‘Microsoft.AspNetCore.Identity.DTO.RegisterRequest’ was missing
required properties, including the following: password at
System.Text.Json.ThrowHelper.ThrowJsonException_JsonRequiredPropertyMissing(JsonTypeInfo
parent, BitArray requiredPropertiesSet) at
System.Text.Json.Serialization.Converters.ObjectWithParameterizedConstructorConverter`1.OnTryRead(Utf8JsonReader&
reader, Type typeToConvert, JsonSerializerOptions options, ReadStack&
state, T& value)
I tried this
const data = { registration: { email: '[email protected]', password:'asdfghjkj' } };
const response = await fetch('https://localhost:7290/register',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': '*/*'
},
body: JSON.stringify(data)
2
Answers
You are missing required properties. The JSON structure you are sending doesn’t match the structure of the
RegisterRequest
object in your web API.Double check your
RegisterRequest
class, then double check the JSON Structure — these two things should match 1 to 1.The JSON you are sending should match the
RegisterRequest
class props without nesting under aregistration
property. Update your client side code to send the correct JSON format as well.You can also add more elegant error handling to catch these errors sooner and in a more verbose way.
Acoroding to your error message, it might be happen due to couple of reason.
First of all, if the request model doesn’t match with your javascript post method structure.
For instance, you are binding your post request model like below:
So upon to your Javascript structure, you should have following API request model, other than you might encounter request failure or error:
Controller action:
Output:
Note: If you have above code, you would be able to resolve your issue. However, If I were the developer, I would have refactored the code as following:
Javascript:
Request Model:
Controller:
Output: