web
You’re offline. This is a read only version of the page.
close
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.

I have the same question (0)
  • Martin Dráb Profile Picture
    237,978 Most Valuable Professional on at

    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.

  • Verified answer
    Mohd saddaf khan Profile Picture
    20 on at

    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.

  • Suggested answer
    jopsuey8425178 Profile Picture
    5 on at

    i dont see the set header.

    request.set_Headers(httpHeader);

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 422 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 239 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans