C
C#2y ago
Ysehporp

❔ WEBAPI [FromBody] Json with optional elements

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
{
"userID": "",
"token": "",
"conversationID": "",
"starttimestamp": "", optional
"endtimestamp": "", optional
}
{
"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."
]
}
}
{
"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)
{
[HttpPost]
[Route("RouteHere")]
public string reqUpdate([FromBody] MessageUpdate req)
{
15 Replies
Ysehporp
YsehporpOP2y ago
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; }

}
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; }

}
Angius
Angius2y ago
Make the optional properties nullable?
Ysehporp
YsehporpOP2y ago
through like an attribute tag do you mean?
Angius
Angius2y ago
Through... making them nullable
Ysehporp
YsehporpOP2y ago
aren't strings nullable?
Angius
Angius2y ago
What version of .NET?
Ysehporp
YsehporpOP2y ago
7
Angius
Angius2y ago
Then no, they're not Unless you manually edited the project file to make reference types nullable by default
Ysehporp
YsehporpOP2y ago
interesting. How can I change what I have to make it nullable then?
Angius
Angius2y ago
string? Like with literally any other type T is not nullable, T? is nullable
Ysehporp
YsehporpOP2y ago
so like
[DefaultValue(null)]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
public string? starttimestamp { get; set; }
[DefaultValue(null)]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
public string? endtimestamp { get; set; }
[DefaultValue(null)]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
public string? starttimestamp { get; set; }
[DefaultValue(null)]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
public string? endtimestamp { get; set; }
not sure why it spaced that all wierdly
Angius
Angius2y ago
That should do the trick, yeah
Ysehporp
YsehporpOP2y ago
Awesome! Let me try it out 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
Angius
Angius2y ago
Nice
Accord
Accord2y 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