0

I have tried to get the same response that I get from Postman from my code.

I have tried the two methods below:

string responsedata = string.Empty;
byte[] dataBytes = Encoding.UTF8.GetBytes(jsonData);

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.ContentLength = dataBytes.Length;
request.ContentType = "application/json";
request.Method = "POST";

using (Stream requestBody = request.GetRequestStream())
{
    requestBody.Write(dataBytes, 0, dataBytes.Length);
}

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
    responsedata = reader.ReadToEnd();
}

return responsedata;

And also tried this:

HttpClient clinet = new HttpClient();

string result = "";
using (var client = new WebClient())
{
    client.Headers[HttpRequestHeader.ContentType] = "application/json";                    
                    
    result = client.UploadString(url, "POST", jsonData);                   
}

return result;

However, both return the error : "The remote server returned an error: (422) Unprocessable Entity" when there is an mistake made in the json. It goes through well if a user does not make a mistake on a wrong form entry.

However, when I run the same "mistake" json on Postman, this is the response I get:

{
    "Error": {
        "code": "NOT_MATCHING_TAXRATE",
        "message": "Error on transaction line 1: Tax rate for HSCode: 0057.11.00 should be '0' instead of '16'."
    }
}

However, the Status in Postman still shows: 422 Unprocessable Entity - The request was well-formed but was unable to be followed due to semantic errors.

What do I need to change to capture the actual error like the above which is very useful to the end user?

Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
Kinyanjui Kamau
  • 1,890
  • 10
  • 55
  • 95

0 Answers0