Click here to Skip to main content
14,738,277 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok, using a .NET application I am POSTing web requests to an API which is poorly supported; will not mention Microsoft by name but it is theirs!

When I POST valid data all is good and I get a HTTP Created response as expected, yippee...

However, when I post invalid data I don't get a response back; instead it throws an HTTP Error 400 Bad request exception with no other messages. And when I say invalid data it could be an invalid date or something not a malformed request.

I have tried catching the exception and webwexception and studied the stack trace but cannot see the reason for the post failing.
If I try the same POST in Postman I do see the reason failing;

"error": {
    "message":"Cannot convert a value to target type 'Edm.Decimal' because of conflict between input format string/number and parameter 'IEEE754Compatible' false/true.  CorrelationId:  6fcac78c-7c2a-41c0-95fa-f1eb9908ca8b."}}

My question is, where does Postman get the error message from? I need to be able to trap this in code so I can determine what is wrong with the data.

Any help would be most appreciated.

What I have tried:

Catching and analysing the exception; googled everywhere.
Updated 23-Jan-20 23:43pm
dnibbo 24-Jan-20 4:41am
Actually the above was a bad example as the JSON I posted was invalid.
However, if I try to create the same record twice I still get the Bad Request error with the below in Postman (still cant catch in code)

"error": {
   "code": "Unknown",
   "message": "The record already exists.  CorrelationId:  862fae85-f342-43a4-96e7-191f0fae561c."

It just looks like the API throws the Bad Request exception if the request fails validation but I just cant see how to catch this in a .NET application.
F-ES Sitecore 24-Jan-20 5:27am
"I need to be able to trap this in code so I can determine what is wrong with the data."

You need to update your code to allow it to do that. Seeing as you didn't think it was relevant to post your code it's hard for us to give concrete advice.
dnibbo 24-Jan-20 7:27am
Thanks but that is the problem, I cannot trap it, the HTTP POST throws an exception and the exception does not contain the error.
The code works just fine, it is the API that is returning Bad Request when it fails validation, it is not a malformed request and not a coding error but a bad API.
As stated, my question is how does Postman get the error when it is not available in the .NET stack trace?
Code posted below but I am certain that is not where the problem is.
jimmson 24-Jan-20 6:00am
4XX errors are client-side errors. So the code would be quite useful to help you.
dnibbo 24-Jan-20 6:58am
Thanks Jimmson but Postman can see the error 'record already exists'.
Only the SERVER would know if the record already existed so it is the server not the client that has decided the request was bad.
dnibbo 24-Jan-20 7:03am
The code is below, the first time I POST the request it works, subsequent POST's fail as the record already exists.

Dim wrResult As HttpWebResponse 
'   post customer details  
Dim sURL As String = _baseURL & "Customer"
Dim WebRequest As WebRequest = HttpWebRequest.Create(sURL)
WebRequest.ContentType = "application/json"
WebRequest.Credentials = GetCredential(sURL)
WebRequest.PreAuthenticate = True
WebRequest.Method = "POST"
Dim sJson As New StringBuilder
sJson.Append("""No"": ""XXX"",")
sJson.Append("""Name"": ""FRED""")
Dim postData As Byte()
Dim utf As New UTF8Encoding
postData = utf.GetBytes(sJson.ToString)
WebRequest.ContentLength = sJson.Length
Using stream = WebRequest.GetRequestStream()
   stream.Write(postData, 0, postData.Length)  
End Using  
wrResult = WebRequest.GetResponse()  
If Not wrResult.StatusCode = HttpStatusCode.Created Then      
    Throw New Exception("Customer XXX not created in Dynamics.")  
End If  
Catch wex As WebException  
End Try

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900