Monday, September 14, 2020

X++ Call rest Api from AX 2012

Now we see how to Authenticate the REST API from AX 2012

  System.Net.WebRequest webreq = System.Net.WebRequest::Create("URL");

    System.IO.Stream streamstr,responsestr;

    System.IO.StreamWriter streamWriter;

    System.Net.WebResponse webresponse;

    System.IO.StreamReader reader;

    System.Exception ex;

   


    str json = "{\"clientId\":\"Value\",\"clientSecret\":\"Value\"}";


   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

    {

       webresponse = webreq.GetResponse();

       responsestr = webresponse.GetResponseStream();

       reader = new System.IO.StreamReader(responsestr);

       info(reader.ReadToEnd());

    }

    catch(Exception::CLRError)

    {

        ex = ClrInterop::getLastException();

        if (ex != null)

        {

            ex = ex.get_InnerException();

            if (ex != null)

            {

                error(ex.ToString());

            }

        }

    }

No comments:

Post a Comment

Please do not add any spam link in the comment box.