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

Consume a webservice REST in AX 2009

(0) ShareShare
ReportReport
Posted on by 50

Hello, 

I would like consume a webservice REST in AX2009 to track the shipment of our orders. 

For this I used this example : Consuming REST APIs in Dynamics AX 2012 (folio3.com)

I modified for my case like this :

 

static void Job37(Args _args)
    {
    System.Net.HttpWebRequest webReq;

    System.Net.HttpWebResponse webRes;

    CLRObject clrObj;

    System.IO.Stream stream;

    System.IO.StreamReader streamRead;

    System.IO.StreamWriter streamWrite;

    System.Net.ServicePoint servicePt;

    System.Net.WebHeaderCollection headers = new System.Net.WebHeaderCollection();

   ;

   //This line gives the user control to run unmanaged and managed code

    new InteropPermission(InteropKind::ClrInterop).assert();

    // Create Object for adding headers

    headers = new System.Net.WebHeaderCollection();



    clrObj = System.Net.WebRequest::Create("https://api.dachser.com/rest/v2/shipmenthistory?tracking-number=4619790347927552&customer-id=231993665126400");

    webReq = clrObj;

    //Set Method Type

    webReq.set_Method("GET");

    webReq.set_KeepAlive(true);

    // Set Content type

    webReq.set_ContentType("application/xml");

    // Add Authorization code

    //headers.Add("Authorization", "sdfgvczzxcbtyfrvb");
    headers.Add("X-IBM-Client-Id", "clientId");
    headers.Add("Accept-Language", "en");
    headers.Add("accept", "application/json");

    //Add header to request

    webReq.set_Headers(headers);

    //Get Service Point

    servicePt = webReq.get_ServicePoint();

    servicePt.set_Expect100Continue(false);

    //Gets the response object

    webRes = webReq.GetResponse();

    //Get Stream

    stream = webRes.GetResponseStream();

    streamRead = new System.IO.StreamReader(stream);

    stream.Close();

    webRes.Close();

}

But the code exits on "webReq.set_Headers(headers);" line. 

I don't know why. Please could you help me ? 

Thanks a lot. 

  • rajeev tiwari Profile Picture
    410 on at
    RE: Consume a webservice REST in AX 2009

    Hi Mimi,

    Can you please post your complete working code , I am also having same issue.

  • mimi51340 Profile Picture
    50 on at
    RE: Consume a webservice REST in AX 2009

    ok no problem I create a new thread.

    THanks

  • Martin Dráb Profile Picture
    231,931 Most Valuable Professional on at
    RE: Consume a webservice REST in AX 2009

    It seems that your originally problem got resolved you're able to call the service. Please verify the answer(s) and don't use this thread for new questions. Instead, please create a new (unanswered) thread about how to present the data to user, give it an appropriate title and explain your requirements to us.

  • mimi51340 Profile Picture
    50 on at
    RE: Consume a webservice REST in AX 2009

    Hello Jonas Kovacs,

    Thanks for your help.

    My problem is I get thisinformation. For example in json :

    2022_2D00_01_2D00_26_5F00_08_2D00_41_2D00_43.png

    In xml : 

    2022_2D00_01_2D00_26_5F00_08_2D00_46_2D00_27.png

    But it is unreadable for a user. 

    I would like to display a clear text. For example : 

    ID : xxxxxxx

    ShipmentDate : 2022-01-25

              Forwarder:

                        PartnerGLN : 4022128000003

                        Name : DACHSER SE Logistikzentrum Allgäu

              AddressInformation:

                        City: Memmingen

                        PostalCode : 87700

                        CountryCode : DE

              ShipmentWeight:

                        Weight : 811.59

                        Unit : kg

    PortOfDeparture: CGC

    PortOfDestination : MUC

    Consignor:

              id:57335219

              partnerGLN : 5607427157528

              names : Sams c/o Taschenbier

              AddressInformation : 

                        Streets : Boulevard de Parc 12

                        City : Coupvray

                        postalCode: 77700

                        CountryCode : FR

    .....

    ....

    in XML on the supplier's API I get this information maybe it is clearer . 

    {
        "shipments": [
            {
                "id": "A8653470034371833856",
                "shipmentDate": "2022-01-24",
                "forwarder": {
                    "id": "6",
                    "partnerGLN": "4022128000003",
                    "names": [
                        "DACHSER SE Logistikzentrum Allgäu"
                    ],
                    "addressInformation": {
                        "city": "Memmingen",
                        "postalCode": "87700",
                        "countryCode": "DE"
                    }
                },
                "shipmentWeight": {
                    "weight": 811.59,
                    "unit": "kg"
                },
                "portOfDeparture": "CGC",
                "portOfDestination": "MUC",
                "consignor": {
                    "id": "84065035",
                    "partnerGLN": "1774679107268",
                    "names": [
                        "Daisy Duck"
                    ],
                    "addressInformation": {
                        "streets": [
                            "Boulevard de Parc 12"
                        ],
                        "city": "Coupvray",
                        "postalCode": "77700",
                        "countryCode": "FR"
                    }
                },
                "consignee": {
                    "id": "57335219",
                    "partnerGLN": "5607427157528",
                    "names": [
                        "Sams c/o Taschenbier"
                    ],
                    "addressInformation": {
                        "streets": [
                            "Bavariafilmpl. 7"
                        ],
                        "city": "Grünwald",
                        "postalCode": "82031",
                        "countryCode": "DE"
                    }
                },
                "references": [
                    {
                        "code": "003",
                        "value": "bmHTYBiz6r"
                    },
                    {
                        "code": "007",
                        "value": "8zW1V6Qulv"
                    },
                    {
                        "code": "HAW",
                        "value": "nYR45812933"
                    }
                ],
                "status": [
                    {
                        "statusSequence": 1,
                        "id": "12090048571",
                        "statusDate": "2022-01-24T04:31:00",
                        "eventSetter": {
                            "id": "6",
                            "partnerGLN": "4022128000003",
                            "names": [
                                "DACHSER SE Logistikzentrum Allgäu"
                            ],
                            "addressInformation": {
                                "city": "Memmingen",
                                "postalCode": "87700",
                                "countryCode": "DE"
                            }
                        },
                        "event": {
                            "code": "Z",
                            "extendedCode": "",
                            "description": "Livré conforme"
                        },
                        "signorOfTheProofOfDelivery": "POTTER"
                    },
                    {
                        "statusSequence": 2,
                        "id": "39703452818",
                        "statusDate": "2022-01-23T08:19:00",
                        "eventSetter": {
                            "id": "250",
                            "partnerGLN": "4046823000007",
                            "names": [
                                "DACHSER Denmark A/S Logistics Centre Copenhagen"
                            ],
                            "addressInformation": {
                                "city": "Hvidovre",
                                "postalCode": "2650",
                                "countryCode": "DK"
                            }
                        },
                        "event": {
                            "code": "A",
                            "extendedCode": "",
                            "description": "Expédié vers le terminal"
                        }
                    },
                    {
                        "statusSequence": 3,
                        "id": "57954173029",
                        "statusDate": "2022-01-21T19:29:00",
                        "eventSetter": {
                            "id": "60",
                            "partnerGLN": "5990034733003",
                            "names": [
                                "LIEGL & DACHSER KFT."
                            ],
                            "addressInformation": {
                                "city": "Pilisvörösvár",
                                "postalCode": "2085",
                                "countryCode": "HU"
                            }
                        },
                        "event": {
                            "code": "E",
                            "extendedCode": "",
                            "description": "Arrivé au terminal"
                        }
                    }
                ]
            }
        ]
    }

    Thanks again for your help. 

  • Suggested answer
    Janos Kovacs Profile Picture
    130 on at
    RE: Consume a webservice REST in AX 2009

    It should be clear now

    static void Job37(Args _args)
    {
        str                             url;
        str                             method;
        str                             header;
        System.Net.HttpWebRequest       httpRequest;
        System.Net.HttpWebResponse      httpResponse;
        System.Net.WebHeaderCollection  headers;
        CLRObject                       clro;
        
        int batchSize = 1024;
        System.IO.Stream receiveStream;
        System.IO.StreamReader readStream;
        System.Text.Encoding encode;
        System.Char[] read;
        System.Text.StringBuilder sb;
        System.String readString;
        str contentEncoding;
    
        int countRead;
    
    
        url = "https://api.dachser.com/rest/v2/shipmenthistory?tracking-number=3616047827517440&customer-id=2716389280317440";
        method = "GET";
    
        try
        {
            clro         = System.Net.WebRequest::Create(url);
            httpRequest  = clro;
    
            headers = new System.Net.WebHeaderCollection();
            headers.Add("X-IBM-Client-Id", "Your API key");
            headers.Add("Accept-Language", "en");
    
            httpRequest.set_Headers(headers);
            httpRequest.set_Method(method);
            httpRequest.set_ContentType("application/json");
    
            httpResponse = httpRequest.GetResponse();
            
            if (httpResponse)
            {    
                receiveStream = httpResponse.GetResponseStream();   
                contentEncoding = httpResponse.get_ContentEncoding();
                
                if (contentEncoding)
                {
                    encode = System.Text.Encoding::GetEncoding(contentEncoding);
                }
                else
                {
                    encode = new System.Text.UTF8Encoding();
                }
    
                readStream = new System.IO.StreamReader(receiveStream, encode);
                read = new System.Char[batchSize]();
    
                countRead = readStream.Read(read, 0, batchSize);
    
                sb = new System.Text.StringBuilder();
                while (countRead > 0)
                {
                    readString = new System.String(read, 0, countRead);
                    sb.Append(readString);
                    countRead = readStream.Read(read, 0, batchSize);
                }
    
                readStream.Close();
                
                info(sb.ToString());
            }
        }
        catch(Exception::CLRError)
        {
            throw error(AifUtil::getClrErrorMessage());
        }
    }

  • mimi51340 Profile Picture
    50 on at
    RE: Consume a webservice REST in AX 2009

    I found the problem. Sorry it is a mistake when I copied/pasted the url.

    In fact we have shipmenthistory and not shipmentstatus in the url.

    Now it works !

    But I still need help please. How to return the result in a readable way in AX? in an information window.

    Thanks again for all the help.

  • mimi51340 Profile Picture
    50 on at
    RE: Consume a webservice REST in AX 2009

    it works with postman so Do you think the problem is in AX ?

  • mimi51340 Profile Picture
    50 on at
    RE: Consume a webservice REST in AX 2009

    I checked in postman and it works :

    2022_2D00_01_2D00_25_5F00_11_2D00_48_2D00_03.png

  • Janos Kovacs Profile Picture
    130 on at
    RE: Consume a webservice REST in AX 2009

    Could you please install the Postman software and run this piece of code. Let me know what the end result will be.

    curl --location --request GET 'https://api.dachser.com/rest/v2/shipmentstatus?tracking-number=1524350993825792&customer-id=5386570691510272' \
    --header 'Accept: application/json' \
    --header 'X-IBM-Client-Id: ' \
    --header 'Accept-Language: en'

  • mimi51340 Profile Picture
    50 on at
    RE: Consume a webservice REST in AX 2009

    Yes sure.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

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

Kudos to the February 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... 293,272 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,931 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans