Skip to main content

Notifications

Small and medium business | Business Central, N...
Suggested answer

How to Pass GUID in header for post request to third party api

Posted on by 119

Hello to all, 
I want to send post request to third party api to retrieve token, where in header pass 'guid' as authorization. Like on picture in postman:

pastedimage1671613588363v2.png



From postman works well, but when I want from business central I got 401 error.

This is my procedure in codeunit:
pastedimage1671613978336v3.png

and on Employee Card I add action where call function from codeunit:
 pastedimage1671614199365v4.png

and got
pastedimage1671614329071v5.png
But if remove 'content-type' I got 401 error.

Do you know where I am wrong? I try to pass  'Bearer' ,'Token', and 'Basic' without coding to base64 but same results

Categories:
  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,748 Super User 2024 Season 1 on at
    RE: How to Pass GUID in header for post request to third party api

    From AL you will typically need to add the token as a header just like Nitin showed you in his code sample.

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: How to Pass GUID in header for post request to third party api

    Hi,

    Try to get an idea from below code.

      var
            bulkArrayPayload: JsonArray;
            JsonText: text;
            Location: record Location;
            RespMessage: text;
            http_Client: HttpClient;
            httpReqMsg: HttpRequestMessage;
            httpResMsg: HttpResponseMessage;
            http_Content: HttpContent;
            http_Header: HttpHeaders;
            serviceResponse: text;
            URL: Text;
        begin
            RespStatus := '';
            Clear(bulkArrayPayload);
            clear(httpResMsg);
            Clear(serviceResponse);
            bulkArrayPayload.Add(JSOObject);
            bulkArrayPayload.WriteTo(JsonText);
            if CopyStr(JsonText, 1, 1) = '[' then begin
                JsonText := CopyStr(JsonText, 2, StrLen(JsonText));
                JsonText := CopyStr(JsonText, 1, StrLen(JsonText) - 1);
            end;
            http_Client.Clear();
            http_Content.Clear();
            http_Content.WriteFrom(JsonText);
            http_Content.GetHeaders(http_Header);
            http_Header.Clear();
            http_Client.DefaultRequestHeaders.Add('Authorization', 'Bearer ' + APIKey + '');
            http_Header.Add('Content-Type', 'application/json');
            if http_client.Post(APIURL, http_Content, httpResMsg) then begin
                httpResMsg.Content.ReadAs(serviceResponse);
                RespStatus := 'Pass';
                if (httpResMsg.HttpStatusCode <> 200) or (StrPos(serviceResponse, 'error') > 0) then
                    RespStatus := 'Failed';
            end;
            exit(serviceResponse);
        end;
  • Haris Korac Profile Picture
    Haris Korac 119 on at
    RE: How to Pass GUID in header for post request to third party api

    hi DAnny3211  thank you very much.

    I wanted to ask you do you know how to pass parameters into httpbody?

    I have that one GUID parameter for authorization into header, but have two parameters (Username, Password) into body. But don't know how to pass that values in AL.

  • Suggested answer
    DAnny3211 Profile Picture
    DAnny3211 9,272 Super User 2024 Season 1 on at
    RE: How to Pass GUID in header for post request to third party api

    hi

    I think the guid problem is over

    this error, on the other hand, I don't know exactly what it is due to

    but I enclose this

    stackoverflow.com/.../could-not-establish-trust-relationship-for-ssl-tls-secure-channel-soap

    check my answer if it helped you, thanks

    DAniele

  • Haris Korac Profile Picture
    Haris Korac 119 on at
    RE: How to Pass GUID in header for post request to third party api

    Yes you are right.

    It seems space in and other non hex letters are problem too.

    Now I try another api but get this error:

    An error occurred while sending the request. The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

    do you know what that could be?

  • Suggested answer
    DAnny3211 Profile Picture
    DAnny3211 9,272 Super User 2024 Season 1 on at
    RE: How to Pass GUID in header for post request to third party api

    hello

    try making the variable GUIDTOK: Guid and not as text

    DAniele

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans