Skip to main content

Notifications

Announcements

No record found.

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

HTTP GET Authorization problem

(0) ShareShare
ReportReport
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!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans