HOW TO EXTRACT HTTP STATUS FROM GRAPH API

Giorgio Sfiligoi 371 Reputation points
2025-04-14T13:10:52.4433333+00:00

When a Graph API encounters an error, how can I extract the HTTP status code in C#?

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,487 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 74,451 Reputation points
    2025-04-14T15:30:27.3+00:00

    I assume use are using the graph sdk and not the rest api. See docs for getting the response code:

    https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/main/docs/errors.md


  2. Rukmini 1,171 Reputation points Microsoft External Staff
    2025-04-18T02:33:26.77+00:00

    Hello @Giorgio Sfiligoi, Since you're using the Microsoft Graph SDK, you're likely expecting exceptions like ODataError or ServiceException when something goes wrong. However, if the internet connection is lost during the request, the SDK throws an HttpRequestException instead — and in this case, StatusCode will be null, and HttpRequestError is marked as Unknown.

    • ODataError is thrown only when the Microsoft Graph API returns a valid HTTP response that includes a structured OData error in the body — for instance, errors like 400 Bad Request, 403 Forbidden, or 404 Not Found.
    • ServiceException is a more general error from the Graph SDK that wraps any error responses received from the API. It includes useful details like the HTTP status code and error message.
    • HttpRequestException, on the other hand, comes from the underlying System.Net.Http layer. It’s thrown when the request fails before it ever reaches the Microsoft Graph service — for example, due to network issues, DNS problems, or no internet connection. Since there's no HTTP response in these cases, there’s no status code available.

    So, in your case, ODataError wasn’t throw because both ODataError and ServiceException depend on the existence of a valid HTTP response from Microsoft Graph. If the network is disconnected and the request can’t reach the service at all, no response is received — and therefore, no structured error can be parsed. ODataError is typically used when there is a response body to analyze, which doesn't apply in this scenario.

    Hence, The HttpRequestException is the correct exception to handle in this scenario, and it indicates a problem with the request itself rather than a server-side error.

    Hope this clarifies things!


    If this answer was helpful, please click "Accept the answer" and mark Yes, as this can help other community members.

    User's image

    If you have any other questions or are still experiencing issues, feel free to ask in the "comments" section, and I'd be happy to help.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.