Call Form URL Encoded Rest API in AL Business Central

Hi,

I’m struggling to call the Form URL encoded rest api in AL Business Central but didn’t find any solution.

Any body can help me and share the sample code?

Thanks.

Hi @Sajjad_Yousuf,

Something like this:

    local procedure PostHttpRequest(Url: Text; Content: Text) Response: Text;
    var
        myHttpClient: HttpClient;
        myHttpResponseMessage: HttpResponseMessage;
        myHttpContent: HttpContent;
        myHttpHeaders: HttpHeaders;
    begin
        // we want to receive a JSON in utf-8 from API
        myHttpClient.DefaultRequestHeaders().Add('Accept', 'application/json');
        myHttpClient.DefaultRequestHeaders().Add('Accept-Charset', 'utf-8');

        myHttpContent.WriteFrom(Content);
        myHttpContent.GetHeaders(myHttpHeaders);
        if myHttpHeaders.Contains('Content-Type') then
            myHttpHeaders.Remove('Content-Type');
        myHttpHeaders.Add('Content-Type', 'application/x-www-form-urlencoded');

        myHttpClient.Post(Url, myHttpContent, myHttpResponseMessage);

        if not myHttpResponseMessage.Content().ReadAs(Response) or not myHttpResponseMessage.IsSuccessStatusCode() then
            ProcessError(Response); // this will depend on the response from API
    end;