-2

I am helping a user get integrated with my API and he is using C# to do so. If he sends me malformed JSON I send him a 422 and then send him the reason for the failure of his request in the response body. The response body seems to be missing in the web exception so he never gets the reason for his error and can't correct it. Where is this response body can I get the WebClient not to throw and exception just because the status code in the response is negative?

using (var clientRequest = new WebClient())
        {
            clientRequest.Headers[HttpRequestHeader.ContentType] = "application/json";
            clientRequest.Headers[HttpRequestHeader.Authorization] = "Token token=" + APITOKEN;

            try
            {
                jsonResponse = clientRequest.UploadString(apiMethodUrl, "POST", json);
                Console.WriteLine("REcived Message from the server");
                Console.WriteLine(jsonResponse);
                Console.WriteLine("See the MEssage");

            }
            catch (WebException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

Can someone help?

Carl Lewis
  • 31
  • 1
  • 5

1 Answers1

-1

You're looking for the WebException.Response property, which returns the complete response.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964