Monday, August 24, 2020

Web API complex input parameter is null

I do maintain a ASP.NET WebAPI 2.0 service that was running smoothly for quite some time on our production server, until one day an existing client of our service reported that they are getting "invalid request" response from our service and that behavior is random. So we checked our logs and found that request object we received from them were actually null and thus the error they got. Till this point our controller action looked like below: 


To diagnose this issue we added logs to check raw request received by us and that confirmed two things: 
1. First request received by was fine but every subsequent same request was landing in our action as null 
2. Raw request content and headers were similar for both successful and failed requests

Below is code that I used to log raw request content:

To sort out the issue we changed our action slightly. Now instead of letting framework to do model binding for us, we started doing it manually as shown in below snippet: 


Here we are reading request content as string and then converting it to our expected object using newtonsoft json converter. Hopefully this will help you to save some of your time.



About Me

My photo
Delhi, India
Fun, music, travel and nature loving, always smiling, computer addict!!