Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Error on calling rest based API from AX 2012 R3

(0) ShareShare
ReportReport
Posted on by 20

I am trying to consume set of web API's, and following (doing everything on test server)

https://dynamics.folio3.com/2014/06/23/consuming-rest-apis-dynamics-ax-2012/

but getting an error as below

The remote server returned an error: (401) Unauthorized. 

Same API works fine when tested with Postman tool.

My code structure

static void TestAPIReq(Args _args)
{
    System.Net.HttpWebResponse      response;
    CLRObject                       clrObj;
    System.Exception                ex;
    System.Net.WebHeaderCollection      httpHeader;
    System.Net.HttpWebRequest           request;
    Notes                               reqBodyStr, resBodyStr;
    
    System.IO.Stream                requestStream, responseStream;
    System.IO.StreamWriter          streamWriter;
    System.IO.StringWriter          stringWriter;
    System.IO.StreamReader          streamRead;

    try
    {
        new InteropPermission(InteropKind::ClrInterop).assert();
        clrObj = System.Net.WebRequest::Create("https://einvoicinggstinAPI/6666666666"); //actuals replaced
        request = clrObj;

        // adding headers
        httpHeader = new System.Net.WebHeaderCollection();
        httpHeader.Add("auth-token", "1.c3f7b1xyzz");//actuals replaced
        httpHeader.Add("prod", "EInvoice");
        httpHeader.Add("owner_id", "xxxxxx");//actuals replaced
        httpHeader.Add("gstin", "6666666666");//actuals replaced
        //request.set_ContentType("application/json");
        request.set_Method("GET");
        
        //Req Stream
        //requestStream = request.GetRequestStream();
        //streamWriter = new System.IO.StreamWriter(requestStream);

        //Prepare Json body
        //this.createJSONStrBody(apiInfo.ApiId);
        //reqBodyStr = "Test";
        
        // writing JSON
        //streamWriter.Write(reqBodyStr);
        //streamWriter.Flush();
        //streamWriter.Close();

        //Response
        response = request.GetResponse(); >>>>point of ERROR
        streamRead = new System.IO.StreamReader(response.GetResponseStream());

        //this.getAPIResponseKeyValues();

        resBodyStr = streamRead.ToString();
        
        info (resBodyStr);

       
    }
    catch
    {
        //exception
        ex = CLRInterop::getLastException().GetBaseException();
        error(ex.get_Message());
    }
}

Tested OK When tested from postman

6811.pastedimage1608197395230v1.png

Aany help would be appreciable.

  • Suggested answer
    jopsuey8425178 Profile Picture
    5 on at
    RE: Error on calling rest based API from AX 2012 R3

    i dont see the set header.

    request.set_Headers(httpHeader);

  • Verified answer
    Mohd saddaf khan Profile Picture
    20 on at
    RE: Error on calling rest based API from AX 2012 R3

    Thanks Martin. It was website blocking issue on server. I got it resolve but there is another issue now? I have updated my post for that..

    The remote server returned an error: (401) Unauthorized.

    Dont understand how the same thing works on postman on same machine but not in AX.

    Note:--

    Got this one resolved. Headers were not added to request.

  • Martin Dráb Profile Picture
    233,066 Most Valuable Professional on at
    RE: Error on calling rest based API from AX 2012 R3

    There seems to be a problem with the SSL certificate. Is it issued by a trusted authority? Isn't it expired?

    Note that this error isn't specific to Dynamics AX, therefore you can find information about it in .NET-related sources.

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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

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,459 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 233,066 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans