[.Net]Flurl Error Handling
When using C# to implement Web APIs in .Net, usually I would use Flurl to interact with external APIs. Sometimes we may encounter an inconsistent response type with different HTTP statuses. (It’s an design issue actually, but we won’t talk about it here.)
Back on topic. In this example, we call an external API to get full name by ID.
When HTTP status is 200, we may receive an object like this :
When HTTP status is 400, we would directly catch an exception, instead of an object like the above. But if we use Postman, we can still see the response like this.
Here the problem comes. The response contains some messages we might need it. But the process directly runs into an exception, how can we access response body. The solution is simple and specify in Flurl. Just handle FlurlHttpException and obtain response JSON in it. So the function would be modified like this :
Another problem solved. 👏👏👏