Search code examples
c#restsharp

RestSharp cannot read response data if status code is 405


I am working with an API that returns a 405 code but also a data object that I need to read to get the errors.

RestSharp always throws an exception when it received the 405 code and I can't see the response body inside the thrown exception.

Take this code for example:

var client = new RestClient("https://webhook.site/c1cf7840-a050-4f22-9fec-c2e915f5b971/")
{
    Options =
    {
        FailOnDeserializationError = false,
        ThrowOnDeserializationError = false,
        ThrowOnAnyError = false
    }
};

var body = new Dictionary<string, object>()
{
    {"test", "value"}
}

var req = new RestRequest("name").AddJsonBody(body);
req.Method = Method.Post;

var response = client.Post(req);

I configured webhook test site to return a 405 and some json. How can I read the returned Json with restsharp?


Solution

  • Answer was to use Execute instead of Post