Announcements
Hi,
so 'm calling a web api that should return for example this in case of 500 (success)
{ "msg": "no msg", "error": "error msg" }
and let's say this in case of error (401,400...etcc)
{ "msg": "msg true", "error": "null" }
when i call the web api, if it was successful, i'll get the msg:msg true and error:null
System.Net.HttpWebResponse response = _request.GetResponse(); using (System.IO.StreamReader streamReader = new System.IO.StreamReader(response.GetResponseStream())) { str webResponse = streamReader.ReadToEnd(); return webResponse; }
However if the call failed, i'll get for example unauthorized error or bad request. But i can't get the error message returned by postman which is
msg:" no msg" and error:"error msg" ... is there a way to get the result when it fails?
His means when he pass request on Postmen he get error with code successfully, and when he pass the parameter in AX or D365fo he got 400 Bad request error.
I am facing the same issue in x++ code dont get proper error but i can see error on Postman.
Hi JuniorAX,
1. I guess you mean error 200 success (not 500).
2. What do you want to is just error handling, wrap your code with try-catch, and in case of error set webResponse to needed text.
Not sure what do you mean by "error message returned by the postman", if you can't be authorized or your request is not properly formed, how the server can return your message (if it's not designed to handle all these cases inside "logic" body)?
public str myMethod() { str webResponse; try { System.Net.HttpWebResponse response = _request.GetResponse(); using (System.IO.StreamReader streamReader = new System.IO.StreamReader(response.GetResponseStream())) { webResponse = streamReader.ReadToEnd(); } } catch //Or catch only WebException and analyze it for protocol error code and exception details { webResponse = { "msg": "no msg", "error": "error msg" }; } return webResponse;
André Arnaud de Cal...
294,118
Super User 2025 Season 1
Martin Dráb
232,866
Most Valuable Professional
nmaenpaa
101,158
Moderator