web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

How to get error message from web api using x++

(1) ShareShare
ReportReport
Posted on by 1,552

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?

I have the same question (0)
  • Suggested answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    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;

  • Hassan najam Profile Picture
    30 on at

    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.

  • CU21081026-5 Profile Picture
    32 on at
    Hey,
    did you get the answer?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 544 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 450 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans