C
C#15mo ago
Hercules

❔ Troubleshooting JSON Mapping for POST Request to UserAsync Endpoint

I'm seeking guidance on formatting the JSON body for a POST request to the following API endpoint: API Signature: csharp
[HttpPost("CreateProjectUser")]
public async Task<ActionResult> CreateProjectUserAsync(HikariUser projectUser, string userName)
[HttpPost("CreateProjectUser")]
public async Task<ActionResult> CreateProjectUserAsync(HikariUser projectUser, string userName)
JSON Body (inputData):
{
"projectUser": {
"Username": "exampleProjectUser",
"Email": "[email protected]",
"Password": "Abc!123",
"Role": "Contributor"
},
"userName": "AM"
}
{
"projectUser": {
"Username": "exampleProjectUser",
"Email": "[email protected]",
"Password": "Abc!123",
"Role": "Contributor"
},
"userName": "AM"
}
The issue I'm encountering is that "userName": "AM" is being incorrectly mapped to HikariUser projectUser.Username, when it should be filling the *exampleProjectUser *field. Additionally, the other properties of HikariUser are not being correctly populated and are showing as null. I've already tried using [JsonPropertyName("")] attributes without success. Any guidance or solutions to ensure the correct mapping of JSON properties to the method parameters would be greatly appreciated.
public class HikariUser
{
[JsonPropertyName("username")]
public string UserName { get; set; }

[JsonPropertyName("email")]
public string Email { get; set; }

[JsonPropertyName("password")]
public string Password { get; set; }

[JsonPropertyName("role")]
public string Role { get; set; }
}
public class HikariUser
{
[JsonPropertyName("username")]
public string UserName { get; set; }

[JsonPropertyName("email")]
public string Email { get; set; }

[JsonPropertyName("password")]
public string Password { get; set; }

[JsonPropertyName("role")]
public string Role { get; set; }
}
3 Replies
Hercules
HerculesOP15mo ago
The solution for this that i made was the following Changed the signature to following
public async Task<ActionResult> CreateProjectUserAsync(**[FromBody]** HikariUser projectUser, string name)
public async Task<ActionResult> CreateProjectUserAsync(**[FromBody]** HikariUser projectUser, string name)
And then i filled the string param wiht string query.
endpoint/CreateProjectUserAsync?name=**John**
endpoint/CreateProjectUserAsync?name=**John**
Pobiega
Pobiega15mo ago
Yeah you need to tell the modelsbinder where to get the data from. If you want to get everything from the body, make a record that has a user and a username
public record CreateProjectUserRequest(HikariUser ProjectUser, string Username);

[HttpPost("CreateProjectUser")]
public async Task<ActionResult> CreateProjectUserAsync(CreateProjectUserRequest request)
public record CreateProjectUserRequest(HikariUser ProjectUser, string Username);

[HttpPost("CreateProjectUser")]
public async Task<ActionResult> CreateProjectUserAsync(CreateProjectUserRequest request)
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server