C
Join ServerC#
help
❔ WEBAPI [FromBody] Json with optional elements
YYsehporp2/12/2023
I am building a .net Wep API application where I will be receiving requests where all of the data is in the JSON body. I understand this is not the preferred way to do this but this is how I have to do it for this application. I will receive a post from my frontend which essentially looks like this
My code is able to process this provided both startTimestamp and endTimestamp are occupied. However as they are optional attributes they will sometimes be absent from requests and in those cases I receive an error
I would like to be able to make these field not required but I am uncertain how.
My method which receives the request starts like this
{
"userID": "",
"token": "",
"conversationID": "",
"starttimestamp": "", optional
"endtimestamp": "", optional
}
My code is able to process this provided both startTimestamp and endTimestamp are occupied. However as they are optional attributes they will sometimes be absent from requests and in those cases I receive an error
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-8e1d4ff06e05e21050e95e3255d1828b-57d0cca122b223fd-00",
"errors": {
"endtimestamp": [
"The endtimestamp field is required."
],
"starttimestamp": [
"The starttimestamp field is required."
]
}
}
I would like to be able to make these field not required but I am uncertain how.
My method which receives the request starts like this
[HttpPost]
[Route("RouteHere")]
public string reqUpdate([FromBody] MessageUpdate req)
{
YYsehporp2/12/2023
and a MessageUpdate looks like this
public class MessageUpdate
{
[JsonProperty(Required=Required.Always)]
public string userID { get; set; }
[JsonProperty(Required = Required.Always)]
public string token { get; set; }
[JsonProperty(Required = Required.Always)]
public string conversationID { get; set;}
[DefaultValue(null)]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
public string starttimestamp { get; set; }
[DefaultValue(null)]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
public string endtimestamp { get; set; }
}
AAngius2/12/2023
Make the optional properties nullable?
YYsehporp2/12/2023
through like an attribute tag do you mean?
AAngius2/12/2023
Through... making them nullable
YYsehporp2/12/2023
aren't strings nullable?
AAngius2/12/2023
What version of .NET?
YYsehporp2/12/2023
7
AAngius2/12/2023
Then no, they're not
AAngius2/12/2023
Unless you manually edited the project file to make reference types nullable by default
YYsehporp2/12/2023
interesting. How can I change what I have to make it nullable then?
AAngius2/12/2023
string?
AAngius2/12/2023
Like with literally any other type
AAngius2/12/2023
T
is not nullable, T?
is nullableYYsehporp2/12/2023
so like
[DefaultValue(null)]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
public string? starttimestamp { get; set; }
[DefaultValue(null)]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
public string? endtimestamp { get; set; }
YYsehporp2/12/2023
not sure why it spaced that all wierdly
AAngius2/12/2023
That should do the trick, yeah
YYsehporp2/12/2023
Awesome! Let me try it out
YYsehporp2/12/2023
Heyyyy! that worked! Thank you so much! My friend is hounding me relentlessly to solve this part of my code and its stressing me out. You're a life saver
AAngius2/12/2023
Nice
AAccord2/13/2023
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.