[.Net]Flurl Error Handling

Ying Tung Wu
1 min readNov 6, 2020

--

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. 👏👏👏

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Ying Tung Wu
Ying Tung Wu

Written by Ying Tung Wu

0 Followers

On the path of learning more.

No responses yet

Write a response