Search code examples
c#asp.net-web-apiconsole-applicationhttp-status-codessystem.web

How do I return the HTTP Status Code from a method in my Web API to main?


I am working on a console app that can execute a few methods from an existing web API. I want the console app to write the status code that is returned, but I am struggling to find a syntax that works.

For example, here is an update method that is currently working as I want it to aside from the web response.

Console.WriteLine("Please enter a valid order item number for the specific data you want to update:");
                    string oNum = Console.ReadLine();

    //more prompts for user to enter data to update

    ...

var update = _service.UpdateOrder(oNum, oProc, oProcDate, oComplete);
Console.WriteLine("Status code: {0}", (update.StatusCode));

I understand why my current status code response doesn't work. I have tried many other things, and I am just stuck as of now. What am I missing?

Edit

Here is the code that _service.UpdateOrder refers to:

public List<Stream> UpdateOrder(string orderID, bool processing, DateTime procDate, bool Complete)
    {
        var request = new RestRequest(StreamUrl, Method.PUT)
        {
            RequestFormat = DataFormat.Json
        };

        request.AddParameter("OrderID", orderID);
        ...


        var response = _client.Execute<List<Stream>>(request);
        if (response.StatusCode == System.Net.HttpStatusCode.Created || response.StatusCode == System.Net.HttpStatusCode.OK)
            return response.Data;
        else
            throw new Exception("Invalid input. Table could not be updated.");

Solution

  • The issue you have is that you are returning a stream from your UpdateOrder method.

    Currently you can only access the StatusCode property inside your UpdateOrder method like you currently are:

    if (response.StatusCode == System.Net.HttpStatusCode.Created)
    

    If you want to access the StatusCode property from your calling code then you need to return IRestResponse from the UpdateOrder like the following:

    public IRestResponse UpdateOrder(string orderID, bool processing, DateTime procDate, bool Complete)
    {
        var request = new RestRequest(StreamUrl, Method.PUT)
        {
            RequestFormat = DataFormat.Json
        };
    
        request.AddParameter("OrderID", orderID);
        ...
    
    
        var response = _client.Execute<List<Stream>>(request);
        if (response.StatusCode == System.Net.HttpStatusCode.Created || response.StatusCode == System.Net.HttpStatusCode.OK)
            return response;
        else
            throw new Exception("Invalid input. Table could not be updated.");
    }
    

    and then in your calling code:

    Console.WriteLine("Please enter a valid order item number for the specific data you want to update:");
                        string oNum = Console.ReadLine();
    
        //more prompts for user to enter data to update
    
        ...
    
    var update = _service.UpdateOrder(oNum, oProc, oProcDate, oComplete);
    Console.WriteLine("Status code: {0}", (update.StatusCode));
    // You can access the List<Stream> from update.Data