RestSharp common issues
RestSharp is a popular rest client in .net
Below are common issues I’d met during development.
- Serialize/Deserialize wrong DateTime data
There was an issue here
Issue: submit data contains DateTime then get wrong data at api.
Submit:
{
"DateOfBirth": "1992-04-16T00:00:00"
}
Receive:
DateOfBirth.ToString("yyyy/MM/dd hh:mm:ss"); // = "1992/04/15 05:00:00"
Reason:
var request = new RestRequest(...);
request.AddJsonBody(data);
Fix:
req.AddParameter("application/json; charset=utf-8", JsonConvert.SerializeObject(data), ParameterType.RequestBody);
Here I use Newtonsoft.Json to serialize the data