Skip to main content

Notifications

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

NAV HttpClient Request Failed

Posted on by
Greeting's 

I have written the code in al for E-Invoice in that I have the authorization and uri everything then also the the frontend part the code throws the error of


I had share the code here please help me to correct my code and get the response with POST method
 
local procedure GetTokenforEInvoice() ResponseText: Text
    begin
 
        Content.GetHeaders(contentHeaders);
        RequestMessage.GetHeaders(RequestHeaders);
        RequestHeaders.Add('gspappid', '87712B75AAE34C4F8DA3BC97BECFD209');
        RequestHeaders.Add('gspappsecret', '1D410F73G5486G4E20GB673G1166661FC8B1');
        RequestMessage.SetRequestUri('https://gsp.adaequare.com/gsp/authenticate?grant_type=token');
        RequestMessage.Method('POST');
        RequestMessage.Content(Content);
        IsSuccessful := HttpClient.Send(RequestMessage, ResponseMessage);
 
        if not IsSuccessful then
            Message('An API call with the provided header has failed.')
        else begin
            ResponseMessage.Content.ReadAs(ResponseText);
            // Message(ResponseText);
            // Rec./Json Txt/ := ResponseText;
            json_Object.ReadFrom(ResponseText);
            if json_Object.Contains('access_token') then begin
                json_Object.Get('access_token', json_Token);
                json_value := json_Token.AsValue();
                Tokenvalueastext := Format(json_value);
            end;
            String := Tokenvalueastext;
            PostmanAccessTokenValue := DelChr(String, '=', '/');
            Message(PostmanAccessTokenValue);
            // RequestHeaders.Add('Authorization', 'Bearer ' + NewString);
        end;
        exit(PostmanAccessTokenValue);
    end;
    //Correct code for getting access token and checking authorization with Post Method on E-Invoice API integration - end
 
    local procedure PostEInvoice() ResponseText: Text
    var
        posteinvoiceinstream: InStream;
    begin
 
        PostmanAccessTokenValue := GetTokenforEInvoice();
        posteinvoiceinstream := convertblobtotext();
        Content.WriteFrom(posteinvoiceinstream);
        Content.GetHeaders(contentHeaders);
        RequestMessage.GetHeaders(RequestHeaders);
        RequestHeaders.Add('Authorization', 'Bearer ' + PostmanAccessTokenValue);
        RequestHeaders.Add('user_name', 'adqgsphpusr1');
        RequestHeaders.Add('password', 'Gsp@1234');
        RequestHeaders.Add('gstin', '02AMBPG7773M002');
        RequestHeaders.Add('requestid', '41bvj8kiqe8u56');
        RequestMessage.SetRequestUri('https://gsp.adaequare.com/test/enriched/ei/api/invoice');
        RequestMessage.Method('POST');
        if ContentHeaders.Contains('Content-Type') then ContentHeaders.Remove('Content-Type');
        ContentHeaders.Add('Content-Type', 'application/json');
        RequestMessage.Content(Content);
 
        IsSuccessful := HttpClient.Send(RequestMessage, ResponseMessage);
        if not IsSuccessful then
            Message('Status Code:%1// Reason : %2', ResponseMessage.HttpStatusCode, ResponseMessage.ReasonPhrase)
        else begin
            ResponseMessage.Content.ReadAs(ResponseText);
            Message(ResponseText);
            Rec./Json Txt/ := ResponseText;
        end;
    end;
 
    trigger OnAfterGetRecord()
    begin
        LargeText := GetLargeText();
    end;
 
    /// <summary>
    /// SetLargeText.
    /// </summary>
    /// <param name=/NewLargeText/>Text.</param>
    procedure SetLargeText(NewLargeText: Text)
    var
        OutStream: OutStream;
    begin
        Clear(Rec./einvoice Txt/);
        Rec./einvoice Txt/.CreateOutStream(OutStream, TEXTENCODING::UTF8);
        OutStream.WriteText(LargeText);
        Rec.Modify();
    end;
 
    /// <summary>
    /// GetLargeText.
    /// </summary>
    /// <returns>Return variable NewLargeText of type Text.</returns>
    procedure GetLargeText() NewLargeText: Text
    var
        TypeHelper: Codeunit /Type Helper/;
        InStream: InStream;
    begin
        Rec.CalcFields(/einvoice Txt/);
        Rec./einvoice Txt/.CreateInStream(InStream, TEXTENCODING::UTF8);
        exit(TypeHelper.TryReadAsTextWithSepAndFieldErrMsg(InStream, TypeHelper.LFSeparator(), Rec.FieldName(/einvoice Txt/)));
    end;
 
    local procedure convertblobtotext() instream: InStream;
    var
        outstream: OutStream;
    begin
        Rec./einvoice Txt/.CreateInStream(instream);
        exit(instream);
    end;

Thank You,
Regards,
Shrey Chauhan
Categories:

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,228 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,056 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans