in the payload class I would change the bool? to bool or string
public bool partial_content { get; set; }
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I am developing an endpoint in C# to accept JSON posted from an external provider (Telnyx). Here is a sample of the data:`
{
"data": {
"event_type": "fax.received",
"id": "e15c28d4-147e-420b-a638-2a2647315577",
"occurred_at": "2021-11-19T16:37:02.863682Z",
"payload": {
"call_duration_secs": 35,
"connection_id": "1771912871052051547",
"direction": "inbound",
"fax_id": "2a168c93-3db5-424b-a408-b70a3da625bc",
"from": "+12399999999",
"media_url": "https://s3.amazonaws.com/faxes-prod/999",
"page_count": 1,
"partial_content": false,
"status": "received",
"to": "+12399999999",
"user_id": "dc6e79fa-fe3b-462b-b3a7-5fb7b3111b8a"
},
"record_type": "event"
},
"meta": {
"attempt": 1,
"delivered_to": "https://webhook.site/27ef892c-c371-4976-ae22-22deea57080e"
}
}
I created a class:
public class DeserializedClass
{
public Data data { get; set; }
public Meta meta { get; set; }
}
public class Payload
{
public int call_duration_secs { get; set; }
public string connection_id { get; set; }
public string direction { get; set; }
public string fax_id { get; set; }
public string from { get; set; }
public string media_url { get; set; }
public int page_count { get; set; }
public bool? partial_content { get; set; }
public string status { get; set; }
public string to { get; set; }
public string user_id { get; set; }
}
public class Data
{
public string event_type { get; set; }
public string id { get; set; }
public DateTime occurred_at { get; set; }
public Payload payload { get; set; }
public string record_type { get; set; }
}
public class Meta
{
public int attempt { get; set; }
public string delivered_to { get; set; }
}
The MVC Controller:
[HttpPost]
public IActionResult InboundFax(DeserializedClass json)
However, any type of processing using json results in a null reference exception. Any help would be appreciated.
in the payload class I would change the bool? to bool or string
public bool partial_content { get; set; }
Thanks for the response. I set up a test endpoint that can be accessed at https://kidsmedicalcare.com:34443/fax/InboundFax. When I post the test data that I posted earlier in this thread, I receive the error System.NullReferenceException: Object reference not set to an instance of an object.at TelnyxExamples.Controllers.FaxController.InboundFax(DeserializedClass json). The endpoint being posted to is a very simple MVC controller:
[HttpPost]
public IActionResult InboundFax(DeserializedClass json)
{
try
{
return Content("OK - " + json.data.event_type.ToString());
}
catch(Exception ex)
{
return Content(ex.ToString());
}
}
The null exception gets thrown at json.data.event_type.ToString(). I have tried other data fields as well and receive the same error message. I don't think I need to change the endpoint to a WebApi controller to accept json data. I am using Postman for the testing, but have received the same error message from the senting api (documentation for the api is https://developers.telnyx.com/docs/v2/programmable-fax/tutorials/receive-a-fax-via-api).
Once again, thanks for the help. I found a solution, but don't really understand why it is a solution. I changed my endpoint to a WebApi in .Net 5.0 and it now works. I thought a MVC controller could handle a JSON data model, but it appears not. Or at least the way my project was set up. Startup.cs for the non working version:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}