Click here to Skip to main content
15,867,851 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": {
    "code":"BadRequest",
    "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.
Posted
Updated 23-Jan-20 22:43pm
v2
Comments
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.

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