RestClient - Request gives back 500 internal server error (content as html) and no ErrorMessage

3k Views Asked by At

I am having issues with a PUT request using REST Client. I tried the same in Postman and I get a ErrorResponse back with a proper error message. So I was expecting the same in the C# - RestSharp Request call. Instead I get a 500 Internal Server Error, with request.ErrorException = null and request.ErrorMessage = null as well. Before anyone accuses me of the Request header, I did set request.AddHeader("Accept", "application/json");

My code is something like this

var client = new RestClient(uri);
var request = new RestRequest(Method.PUT);
request.AddHeader("Accept", "application/json");`
request.AddParameter("xx...", value); 
var response = client.Execute(request);
var content = response.Content;   

So now the response.Content I see is the following html (I will just post partial content for information)

<html>
    <head>
        <title>Runtime Error</title>
        <meta name="viewport" content="width=device-width" />
        <style>
         body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} 
         p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
         b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
         H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
         H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
         pre {font-family:"Consolas","Lucida Console",Monospace;font-size:11pt;margin:0;padding:0.5em;line-height:14pt}
         .marker {font-weight: bold; color: black;text-decoration: none;}
         .version {color: gray;}
         .error {margin-bottom: 10px;}
         .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
         @media screen and (max-width: 639px) {
          pre { width: 440px; overflow: auto; white-space: pre-wrap; word-wrap: break-word; }
         }
         @media screen and (max-width: 479px) {
          pre { width: 280px; }
         }
        </style>
    </head>

    <body bgcolor="white">
.....

....
</html>

For now in my wrapper method I am checking to see if the StatusCode is not a 200, then if the request.ErrorException is null, in this case to just show a generic message 'something went wrong... ' type of a thing. This is just a bandaid solution for now. I would like to know how to get the exact Error Response or how to handle this situation.

Any help is greatly appreciated.

1

There are 1 best solutions below

0
On

In web services the actual error will never be returned unless you explicitly set the error message to one of its properties. For example in WebAPI you can return error message by using Request.CreateResponse() method. The method needs to return HttpResponseMessage type. Following is the code

[HttpPut]
public HttpResponseMessage MyPutMethod()
{
try
{
...process
return Request.CreateResponse(HttpStatusCode.OK, "Success");
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}

NOTE: It is not a good idea to send exception details for security reasons.