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, ...
Unanswered

Consume Web Service (JSON) in AX 2012

(0) ShareShare
ReportReport
Posted on by

Hi,

I'm trying to call a webservice (POST input JSON) in X , below is my code and I'm not sure what is wrong/missing:

static void Job269(Args _args)
{
    System.Net.WebHeaderCollection  headers = new System.Net.WebHeaderCollection();
    System.IO.Stream streamstr,responsestr;
    System.IO.StreamWriter streamWriter;
    System.Net.WebResponse webresponse;
    System.IO.StreamReader reader;
    System.Exception ex , ex2, webexception;
    System.Net.WebRequest webreq = System.Net.WebRequest::Create("url");

    str json = @'{"items":[
    {"product_sku":"SKU1","quantity":"15"},
    {"product_sku":"SKU2","quantity":"3"} ]}';
    
    headers.Add('Authorization: bearer AuthorizationCODE');
    webreq.set_Headers(headers);

    webreq.set_Timeout(100000);

    webreq.set_Method("POST");
    webreq.set_ContentType("application/json");


   streamstr = webreq.GetRequestStream();
   streamWriter = new System.IO.StreamWriter(streamstr);
   streamWriter.Write(json);
   streamWriter.Flush();
   streamWriter.Close();
   streamWriter.Dispose();

    try
    {
        //info('Line 42');
        webresponse = webreq.GetResponse();

       responsestr = webresponse.GetResponseStream();
       reader = new System.IO.StreamReader(responsestr);
       info(reader.ReadToEnd());
    }
    /*
    catch(Exception::CLRError)
    {
        ex = ClrInterop::getLastException();
        //ex2 = CLRInterop::getLastException().GetBaseException();
        if (ex != null)
        {
            ex = ex.get_InnerException();
            if (ex != null)
                error(ex.ToString());
        }
    }*/
    catch (Exception::CLRError)
{
ex = CLRInterop::getLastException();
info(ex.ToString());
if (ex != null)
{
ex = ex.get_InnerException();
if ((ex != null) && (ex is System.Net.WebException))
    info(ex.ToString());
}

}
}

Yet I'm getting the following error (not sure why):

pastedimage1665247673847v1.png

Moreover, following is the call of the webservice from Postman:

pastedimage1665247947080v2.png

Any idea what's the error or how to call such services with authorization?

I have the same question (0)
  • Martin Dráb Profile Picture
    236,297 Most Valuable Professional on at
    RE: Consume Web Service (JSON) in AX 2012

    What result do you get when you actually send the request from Postman? I assume it succeeds, but you didn't say it.

    If it's the case, please try the same value of the Authorization header in X++.

    The HTTP code seems to suggest that the server can't give you a response time that you can accept, but it's not clear to me why.

    By the way, you should dispose resources correctly, which means all disposable resources and both in the successful scenario and on failure.  Also, calling both on Close() and Dispose on streamWriter isn't needed - they do the same thing.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

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

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 2,167

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 617 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans