Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Unanswered

AX2009 bearer token - why I got empty authorization property in header?

Posted on by 1,085

Hi everyone,

i'm trying to send a GET request with bearer token auth. The "parameters" of the request are pretty simple (for obious reasons i'm not typing the real host and token here..):

-host: "http://myhost.it/rest/V1/order/id?id=22"

-Authorization : "Bearer 116zjy7buwy80ax2s7id71ian9givs11"

-method: GET

-content type: "application/json"

static void job001(Args _args)
{
    System.Net.HttpWebRequest       request;
    System.IO.Stream                dataStream;
    System.Net.WebHeaderCollection  httpHeader;
    CLRObject                       clrObj;

    str                             token = "116zjy7bu1180a11s7id71ian9givs11";
    ;

    try
    {
        new InteropPermission(InteropKind::ClrInterop).assert();
        clrObj = System.Net.WebRequest::Create("http://myhost.it/rest/V1/order/id?id=22");

        httpHeader = new System.Net.WebHeaderCollection();

        httpHeader.Add("UserAgent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0");
        httpHeader.Add("Authorization", "Bearer "   token);
        httpHeader.Add("ContentType","Application/json");

        request = clrObj;
        request.set_Method("GET");

        request.set_Headers(httpHeader);
        
        //debug - begin
        httpHeader = request.get_Headers();
        info(httpHeader.ToString());        //1
        info(httpHeader.Get("Authorization"));  //2
        info(httpHeader.Get("UserAgent"));   //3
        //debug - end

        request.GetResponse();
    }
    catch(Exception::CLRError)
    {
        throw error(AifUtil::getClrErrorMessage());
    }
}

The host returns a "The remote server returned an error: (401) Unauthorized." error.

I've tried sending the same request via this site https://reqbin.com/ and it works perfectly.

So i did little more debugging and i asked to the host administrator to show me the header received from Ax 2009: the header looks like this:

Host: myhost.it

Contenttype: Application/json

Useragent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0

Authorization:

Looks like the "Authorization" property gets empty once the request is made.

By the way, the debug section in my code prints out every property of the header and they are all filled properly. The "Authorization" property too.I've also tried "httpHeader.Set" instead of "httpHeader.Add"...same result.

I'm really having a hard time on this strange behaviour... any help would be really appreciated.

Thank you in advance

regards

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 9th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,277 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,135 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans