简体   繁体   中英

RestSharp is throwing a bad request on a GET call, but is missing response body

I am making a GET call to an external REST API. I was putting in a parameter incorrectly and was getting an HttpRequestException. When catching the exception the only details it returns are:

Request failed with status code BadRequest

When monitoring the traffic using Fiddler I can see the server was returning a json message in the body explaining why the call failed.

Is there anyway to access this message from RestSharp?

I am using .net 6 and RestSharp version 108.0.2

you must set ThrowOnAnyError from RestClientOptions to false :

private async Task<T?> SendRequest<T>()
        {
            string url = "your request url";
            var options = new RestClientOptions(url)
            {
                ThrowOnAnyError = false,
                MaxTimeout = 6000
            };
            var client = new RestClient(options);
            var request = new RestRequest().AddHeader("Content-Type", "application/json");
            request.Method = Method.Post;
            var response = await client.ExecuteAsync<T>(request);
            return response.Data;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM