Avoid deserialization for server response

I’m dealing with the communication with a legacy system throught REST services

I’m using JsonClient to Post data with the following code

                    string rawResp;
                    try
                    {
                        using (HttpWebResponse resp = await base.PostAsync<HttpWebResponse>(targetUrl, requestDto, cancelToken))
                        {
                            rawResp = resp.ReadToEnd();
                        }
                    }
                    catch (Exception ex)
                    {
                        ... // error handling
                    }

the code breaks at await base.PostAsync(....
the exception I get is type WebServiceException and the InnerException.Message is

Type definitions should start with a '{', expecting serialized type 'ErrorResponse', got string starting with: <html><head>.....

I was expecting HttpWebResponse doesn’t force deserializations but it’s happening

is there a way to avoid the client deserialization at all?

ovverring the Format and setting text doesn’t help

thanks

This error is because the API is failing and it’s not returning the expected error response.

Your APIs can have IReturn<string> in which case it wont deserialize it, but if the API fails it’s still expects to receive a serialized error response.

If your APIs are returning HTML you should use a generic HTTP Client like HTTP Utils or HttpClient directly instead of a ServiceClient.