web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Unanswered

NAV HttpClient Request Failed

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

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 755 Super User 2026 Season 2

#2
YUN ZHU Profile Picture

YUN ZHU 413 Super User 2026 Season 2

#3
AndrewThomas81 Profile Picture

AndrewThomas81 387 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans