Wawa Wawa - 4 months ago 11
C# Question

C# Restsharp - How to verify the content result is unexpected result

Code to get the content result

IRestResponse response = client.Execute(request);
var content = (string)JsonConvert.DeserializeObject(response.Content);


The
response.content
should return ether one of this 4 msg.


  • OK

  • INVALID_MESSAGE_ID

  • MESSAGE_NOT_FOUND

  • INTERNAL_ERROR



What if the API returning something else, how can I verify that?

Answer

Check the content:

if(content != "OK" && content != "INVALID_MESSAGE_ID" && content != "MESSAGE_NOT_FOUND" && content != "INTERNAL_ERROR") {
    //Do some handling, like logging
}

Or:

switch(content) {
    case "OK":
        // Do something, when content is OK
        break;
    case "INVALID_MESSAGE_ID":
        // Do something, when content is INVALID_MESSAGE_ID
        break;
    case "MESSAGE_NOT_FOUND":
        // Do something, when content is MESSAGE_NOT_FOUND
        break;
    case "INTERNAL_ERROR":
        // Do something, when content is INTERNAL_ERROR
        break;
    case default:
        // Do something, if no case is matched
        break;
}