Skip to main content

Notifications

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?

  • CU21081026-5 Profile Picture
    32 on at
    How to get error message from web api using x++
    Hey,
    did you get the answer?
  • Hassan najam Profile Picture
    30 on at
    RE: How to get error message from web api using x++

    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.

  • Suggested answer
    Sergei Minozhenko Profile Picture
    23,091 on at
    RE: How to get error message from web api using x++

    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;

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,118 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,866 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans