Skip to main content

Notifications

Announcements

No record found.

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

Sending a POST Request in AL code for Stripe Checkout API

Posted on by 2,203 Super User 2024 Season 1

I'm trying to create a Stripe checkout session in Business Central. but I'm facing an issue with passing the content type as "application/x-www-form-urlencoded". I've tried setting the header  header.Add('Content-Type', 'application/x-www-form-urlencoded') but I'm facing this error.
pastedimage1676309836993v1.png

This is my Code..

procedure StripeAPI()
    Var
        client: HttpClient;
        cont: HttpContent;
        header: HttpHeaders;
        response: HttpResponseMessage;
        Jobject: JsonObject;
        tmpString: Text;
        TypeHelper: Codeunit "Type Helper";
        granttype: text;
        clienid: text;
        username: text;
        password: text;
    Begin
        CancelURL1 := 'https://example.com';
        success_url1 := 'https://example.com';
        line_items_currency1 := 'usd';
        line_items_Nam := 'Shirt';
        line_items_unitAmoun := '2000';
        LineAmountQuantit := '1';
        Mode1 := 'payment';
        PaymentMethodType1 := 'card';

        tmpString := 'mode='   TypeHelper.UrlEncode(Mode1)   '&line_items[0][price_data][unit_amount]='   TypeHelper.UrlEncode(line_items_unitAmoun)   '&cancel_url='   TypeHelper.UrlEncode(CancelURL1)   '&success_url='   TypeHelper.UrlEncode(success_url1)   '&line_items[0][price_data][currency]='   TypeHelper.UrlEncode(line_items_currency1)   '&line_items[0][price_data][product_data][name]='   TypeHelper.UrlEncode(line_items_Nam)   '&line_items[0][quantity]='   TypeHelper.UrlEncode(LineAmountQuantit)   '&payment_method_types[0]='   TypeHelper.UrlEncode(PaymentMethodType1);

        Message(tmpString);
        cont.WriteFrom(tmpString);
        cont.ReadAs(tmpString);

        cont.GetHeaders(header);
        header := client.DefaultRequestHeaders;
        header.Add('charset', 'UTF-8');
        header.Remove('Content-Type');
        header.Add('Content-Type', 'application/x-www-form-urlencoded');
        header.Add('Authorization', 'Bearer *********************************************');

        client.Post('**********************', cont, response);
        response.Content.ReadAs(responseText);
    end;


I have also checked that the tmpString a variable that contains the data in the text format url embedded and it is working fine in Postman when selecting "application/x-www-form-urlencoded".

can you guys help me..
Regards

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: Sending a POST Request in AL code for Stripe Checkout API

    Good to know.

  • Govinda Kumar Profile Picture
    Govinda Kumar 2,203 Super User 2024 Season 1 on at
    RE: Sending a POST Request in AL code for Stripe Checkout API

    Hi, Nitin Verma

    Yes, it is working.

    Regards

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: Sending a POST Request in AL code for Stripe Checkout API

    So its working now?

  • Govinda Kumar Profile Picture
    Govinda Kumar 2,203 Super User 2024 Season 1 on at
    RE: Sending a POST Request in AL code for Stripe Checkout API

    Hi, Nitin Verma thank you very much for helping...

    I just replaced HttpHeaders with HttpClient, It was my mistake in the previous reply.. sorry for that..

    pastedimage1676355648629v1.png
    Thank you
    regards

  • Govinda Kumar Profile Picture
    Govinda Kumar 2,203 Super User 2024 Season 1 on at
    RE: Sending a POST Request in AL code for Stripe Checkout API

    Hi Nitin Verma,

    Thank you for replying..

    I tried using your way, but getting the error..
    pastedimage1676355084135v1.png

    I also tried that one
    pastedimage1676355135684v2.png

    Can you help kindly tell me what should I do..
    regards

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: Sending a POST Request in AL code for Stripe Checkout API

    Hi,

    Try to use as per below

    cont.WriteFrom(tmpString);

    cont.getheaders(Header);

    header.clear();

    header.DefaultRequestHeaders.Add('Authorization', 'Bearer ' + APIKey + '');

    header.Add('Content-Type', 'application/x-www-form-urlencoded');

    client.Post('**********************', cont, response);
    response.Content.ReadAs(responseText);

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 Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans