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

Community site session details

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

read api response

(4) ShareShare
ReportReport
Posted on by 1,847
Hi,

when i call an api, the response of this call might sometimes result in a different namespaces, but the field names are still the same.

How to read the "errorDescription" field regardless if the namespace is SOAP-1 and ns4 OR if it's SOAP-2 and ns5?? how to do it in x++?

So i have now xmlDocument.loadXml(xml);
what to do next?




Categories:
I have the same question (0)
  • Martin Dráb Profile Picture
    235,979 Most Valuable Professional on at
    read api response
    I see your code, but I don't know what you want your code to do. Why do you handle all exceptions in execute() just to throw a new exception a moment later and immediatelly catch the exception you've just cought and check its type (which can't by anything else than the X++ exception you've just raised)? The other exception makes no sense to me - you have all the information before even throwing it. You could do it all in execute().
     
    Please tell us the type of the exception. We can't know which of your catch blocks are executed without information about the exception type. You can also use the debugger to check parts of your code are executed and which are ignored.
     
    Also, what logic do you have in getXMLString()?
  • .. Profile Picture
    1,847 on at
    read api response
    Hi @Martin Dráb

    In order to use what you suggested, what shall i remove from this code? and what to replace what u suggested with?
     public void process()
     {
         if (this.canProcess())
         {
             try
             {
                Class1 class1;
                class1 = this.getClass1();
                if(!class1.execute())
                { 
                    throw error("error");
                }
             }
             catch (Exception::CLRError)
             {
    
                 using (xConnection connection = new xConnection(false))
                 {
                     connection.tts_begin();
    
                     this.submitError();
    
                     connection.tts_commit();
                 }
             }
             catch  // it goes here
             {
                 using (xConnection connection = new xConnection(false))
                 {
                     connection.tts_begin();
    
                     this.submitError();
    
                     connection.tts_commit();
                 }
             }
         }
     }
    
    
    
    protected boolean execute()
    {
        System.Net.WebException webException;
        boolean ret;
    
        errorResponseBody = '';
        try
        {
            new InteropPermission(InteropKind::ClrInterop).assert();
    
            webRequest = System.Net.WebRequest::CreateHttp(this.buildRequestURL());
    
            str token = this.getAccessToken();
            if (token)
            {
                webRequest.get_Headers().Add('Authorization', strFmt('Bearer %1', token));
            }
    
            this.prepareHttpRequest();
    
            webResponse = webRequest.GetResponse();  //it fails here
    
            this.processResponse();
        
            ret = true;
        }
        catch (webException)//it goes here
        {
            if (webException.Response)
            {
                webResponse = webException.Response;
                System.IO.Stream responseStream = webResponse.GetResponseStream();
                if (responseStream)
                {
                    System.IO.StreamReader reader   = new System.IO.StreamReader(responseStream);
                    errorResponseBody               = reader.ReadToEnd();
                }
            }            
            Error(errorResponseBody? errorResponseBody : webException.Message);
        }
        catch  
        {
            // Show errors in the infolog if they are not there already.
            str clrErr = APLUtilFunctions::processCLRErrorExceptionToString();
            if (clrErr)
            {
                Error(clrErr);
            }
        }
        finally
        {
            if (webResponse)
            {
                webResponse.Dispose();
            }
            CodeAccessPermission::revertAssert();
        }
    
        return ret;
    }
    
    
    public void submitError()
    {
         ttsbegin;
         str xml = this.getXMLString();
         if (xml)
         {
            xmlDocument   = new XMLDocument();
    
            if (xmlDocument.loadXml(xml));
            {
               XmlNamespaceManager xmlNameSpaceManager;
               XmlNode             errorXNode;
    
               xmlNameSpaceManager = new XmlNamespaceManager(xmlDocument.nameTable());
               xmlNameSpaceManager .addNamespace('SOAP-1', 'http://schemas.xmlsoap.org/soap/envelope/');
               nsManager.addNamespace('ppp'     , 'http://www.w3.org/2005/08/addressing');
               nsManager.addNamespace('ns4'     , 'http://xmlx.xx.com/X');
               
               errorXNode = xmlDocument.selectSingleNode('//SOAP-1:Error/details/ns4:ErrorX', xmlNameSpaceManager);
               if (errorXNode)
               {
    	       str error = this.getXMLElementValue(ErrorX, 'ns4:errorDescription');	
               }
          }
          //logic
          ttscommit;
    }
     
  • Martin Dráb Profile Picture
    235,979 Most Valuable Professional on at
    read api response
    When the service response with a fault contract, the client library will thrown an exception. You can use the catch statement to get the exception object and then you can work with its properties such as Message.
  • .. Profile Picture
    1,847 on at
    read api response
    Hi Martin,
     
    I read the article, but i don't quite understand what i should do now to get the value inside the "error description" field regardless of how the response look like?
     
    To give you a background of what i'm doing
     
    I'm calling an api, where i'm filling the body of the request by creating an xml filled from d365 data.
    Now this api call will produce a response, and i would like to take a value from that response and store in a field.
     
    The link you gave me is for the response part right? 
    But can you give more explanation of what i should do exactly? More clear steps would be helpful 
  • Suggested answer
    Martin Dráb Profile Picture
    235,979 Most Valuable Professional on at
    read api response
    You normally shouldn't be working with raw SOAP messages at all. You should generate client classes and work with these classes, therefore the implementation details are dealt with for you. It seems that you've artificially created the problem by developing something that you shouldn't be developing, because it's already done and in better than what you've done by yourself.
     
    To see the correct steps, check the documentation page Consume external web services, for instance. Of course, some knowledge of SOAP and WCF may be still needed, if you want to use non-default configuration. 
  • .. Profile Picture
    1,847 on at
    read api response
    Hi,

    just to add to my question, the code I did here was to handle one response, but is there a way to handle both responses? is there a better way to write the code?
     
    
    
    
    What i noticed is that errorXNode is filled correctly regardless if the response contains "SOAP-1 and ns4" or if it's "SOAP-2 and ns5".
    Even though the path specified in the selectSingleNode is SOAP-1 and ns4, how did that work?
    
    it only fails in getXMLElementValue where it's expecting ns5, but the code says ns4
    
    

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

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

#1
Sohaib Cheema Profile Picture

Sohaib Cheema 844 User Group Leader

#2
André Arnaud de Calavon Profile Picture

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

#3
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 553

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans