I am making an API call to a third party API, the code snippet was made to make a request and extract the response body from the response into a string in .Net 3.1. I can parse successful responses normally and successfully. We were able to parse WebException responses up to yesterday, where now they just return garbled string down below.
public dynamic RequestSender(HttpWebRequest request, dynamic vm)
{
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
Byte[] byteArray = encoding.GetBytes(JsonConvert.SerializeObject(vm));
request.ContentLength = byteArray.Length;
request.ContentType = @"application/json; ";
try
{
System.IO.Stream os = request.GetRequestStream();
os.Write(byteArray, 0, byteArray.Length); //Push it out there
os.Close();
System.Net.WebResponse resp = request.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
var responseString = sr.ReadToEnd().Trim();
return responseString;
}
catch (WebException webErr)
{
return webErr;
System.IO.StreamReader sr = new System.IO.StreamReader(webErr.Response.GetResponseStream());
var responseString = sr.ReadToEnd().Trim();
return responseString;
}
catch (Exception err)
{
return err;
}
}
The expected response should be:
{
"response": {
"errors": [
{
"code": "111285",
"message": "The postal code F3FKWFIOJEF is invalid for CA United States."
}
]
}
}
However we are getting this as a response:
"\u001f�\b\0\0\0\0\0\0\u0003\u0014�A\n�0\u0010\u0005Ы\f�\u00161�������E�|�@L�L\u0012)�w�n\u001f��\n[r2��\u0019�Y��G�)���9w:_��o��ן�3��K��W�\"J����\"9�\u0018�䛗��\b�J���A\U00069c02@\u0006m2���s�~\0\0\0��\u0003\0\u0010A�(\u007f\0\0\0"
What we've tried:
We've tried altering the request, altering the encoding that streamreader uses to parse the response body, alongside making sure that the response body has length, which it does.
Not sure what it could be as the third party API is unusual in its implementation in that it attaches a response body into a 400 Bad Request error.