Skip to main content

Notifications

Small and medium business | Business Central, N...
Answered

HTTP GET Authorization problem

Posted on by 65

Hello!

I have a problem with a GET query.

POST queries work without any problem while GET has an authorization error. My code:

procedure GetProductInfo(_ItemId: Integer; _Token: Text)
var
    Client: HttpClient;
    Request: HttpRequestMessage;
    Response: HttpResponseMessage;
    Headers: HttpHeaders;
    Uri: Text;
    FullToken: Text;
    ResponseTxt: Text;
    Json: JsonObject;
begin
    Uri := 'https://myaddress.com/product/'   Format(_ItemId);
    FullToken := 'Bearer '   Format(_Token);

    Request.Method := 'GET';
    Request.SetRequestUri(Uri);
    Headers.Add('Authorization', FullToken);
    Request.GetHeaders(Headers);
    Client.Send(Request, Response);

    if Response.IsSuccessStatusCode() then begin
        Response.Content.ReadAs(ResponseTxt);
        Json.ReadFrom(ResponseTxt);

        ProductId := GetJsonTextField(Json, 'product_id');
        ProductCode := GetJsonTextField(Json, 'code');
        Evaluate(Stock, GetJsonTextField(Json, 'stock'));

        Message('Good');
    end
    else
        Message('Bad');
end;

Error: HTTP 401

Everything works fine in Postman.

Best Regards

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: HTTP GET Authorization problem

    Great.

  • Alzack18 Profile Picture
    Alzack18 65 on at
    RE: HTTP GET Authorization problem

    Thank you for response! I changed

    Headers.Add('Authorization', FullToken);

    to

    Client.DefaultRequestHeaders.Add('Authorization', FullToken);

    Problem solved!

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: HTTP GET Authorization problem

    Hi,

    Please try this

           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.get(APIURL, httpResMsg) then

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!

Community AMA December 12th

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

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 229,963 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans