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

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

Using a x-www-form-urlencoded Rest API with Business central (to get token)

(1) ShareShare
ReportReport
Posted on by 60

Hi everyone,

I'm trying to develop a function with AL that permits to Post a url encoded content.
pastedimage1612980029302v1.png

It works fine with postman. The following procedure that I've started developping doesn't work :

procedure GetToken() ResponseText: text;

    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
        granttype := 'xxxxx';
        clienid := 'xxxxxx';
        username := 'xxxxxxxxx';
        password := 'xxxxxxxxxxxxx';
        Jobject.Add('grant_type', TypeHelper.UrlEncode(granttype));
        Jobject.Add('client_id', TypeHelper.UrlEncode(clienid));
        Jobject.Add('username', TypeHelper.UrlEncode(username));
        Jobject.Add('password', TypeHelper.UrlEncode(password));
        Jobject.WriteTo(tmpString);
        cont.WriteFrom(tmpString);
        cont.ReadAs(tmpString);

        cont.GetHeaders(header);
        header.Add('charset', 'UTF-8');
        header.Remove('Content-Type');
        header.Add('Content-Type', 'application/x-www-form-urlencoded');

        client.Post('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', cont, response);
        response.Content.ReadAs(ResponseText);

    end;

Can someone help me please?
I get this response everytime :

{
"error": "invalid_request",
"error_description": "Missing form parameter: grant_type"
}

Ps : it doesn't work even without encoding the parameters.

Thanks in advance

I have the same question (0)
  • Suggested answer
    Marco Mels Profile Picture
    on at

    Hello,

    We currently do not have dedicated Dev support via the Dynamics 365 Business Central forums, but I wanted to provide you some additional resources to assist.  If you need assistance with debugging or coding I would recommend discussing this on one of our communities.

    www.yammer.com/dynamicsnavdev

    dynamicsuser.net/.../developers

    I will open this up to the community in case they have something to add.

    Thanks.

  • Community Member Profile Picture
    on at

    @Brahim Nasr I am facing the same issue have you got any solution for the same?

  • RickTweedle Profile Picture
    5 on at

    Did you get this resolved? I'm having the same issue re "missing parameter grant_type" when trying to get an access token in AL code.

    I've also tried sending as json, and form-data.. both return the same error.

    I've noticed that postman only seems to work if send as form-data, if i convert it to application/json and send the details as raw json data i get the same "missing parameter grant_type" error

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,229

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,867 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,153 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans