Trying to set up a codeunit to consume External REST API

garmoogarmoo Member Posts: 21
Hi! I'm trying to learn a bit about APIs but not getting very far. Can anyone help me?

I had a browse for some APIs I could mess around with and decided to use this one as it seemed to have a lot of documentation. Free Public football API. I'm struggling to get my code to work with it however. Currently I am only trying to check that I can connect to it without errors... but I can't even get past the first hurdle. I looked over a few blogs on the subject but I'm still not sure where I'm going wrong.

This is my codeunit
procedure GetAPIData(Endpoint: Text; Header1Value: Text; Header2Value: Text)
    var
        HttpClient: HttpClient;
        HttpResponse: HttpResponseMessage;
        ResponseText: text;
        Uri: Text;
        Header1: Text;
        Header2: Text;
    begin
        //Uri := Endpoint + APIKey;
        Uri := Endpoint;
        if HttpClient.Get(Uri, HttpResponse) then
            // HttpResponse.Headers().Add(Header1, Header1Value);
            // HttpResponse.Headers().Add(Header2, Header2Value);
            if HttpResponse.IsSuccessStatusCode() then begin
                HttpResponse.Headers().Add(Header1, Header1Value);
                HttpResponse.Headers().Add(Header2, Header2Value);
                HttpResponse.Content().ReadAs(ResponseText);
                Message(ResponseText);
            end;
    end;

but I am getting this error
x7zuw7of5nii.png

Has anyone any idea of what I'm doing wrong? Or any links to good blogs that explains the process in simple terms?

Thanks!

Best Answer

  • ResolusResolus Member Posts: 40
    Answer ✓
    Well, if you want to add headers, you should do to before you do the request.
    Adding them to the response won't help that much I think.

    And ofcourse, if you want to add headers, you must make sure they have a value.
    You used Text variables, but they are empty and thus you get the error that the header parameter name is null or empty.

    Haven't tried the code below, but might guide you in the right direction.
    Tomorrow I could check further if needed.
    procedure GetAPIData(Endpoint: Text; Header1Value: Text; Header2Value: Text)
        var
            HttpClient: HttpClient;
            HttpResponse: HttpResponseMessage;
            HttpRequest: HttpRequestMessage;
            HttpHeader: HttpHeaders;
            ResponseText: Text;
            Header1: Label 'x-rapidapi-key', Locked = true;
            Header2: Label 'x-rapidapi-host', Locked = true;
        begin
            HttpHeader := HttpClient.DefaultRequestHeaders();
            HttpHeader.Add(Header1, Header1Value);
            HttpHeader.Add(Header2, Header2Value);
    
            if HttpClient.Get(Endpoint, HttpResponse) then
                if HttpResponse.IsSuccessStatusCode() then begin
                    HttpResponse.Content().ReadAs(ResponseText);
                    Message(ResponseText);
                end;
        end;
    

Answers

  • ResolusResolus Member Posts: 40
    Answer ✓
    Well, if you want to add headers, you should do to before you do the request.
    Adding them to the response won't help that much I think.

    And ofcourse, if you want to add headers, you must make sure they have a value.
    You used Text variables, but they are empty and thus you get the error that the header parameter name is null or empty.

    Haven't tried the code below, but might guide you in the right direction.
    Tomorrow I could check further if needed.
    procedure GetAPIData(Endpoint: Text; Header1Value: Text; Header2Value: Text)
        var
            HttpClient: HttpClient;
            HttpResponse: HttpResponseMessage;
            HttpRequest: HttpRequestMessage;
            HttpHeader: HttpHeaders;
            ResponseText: Text;
            Header1: Label 'x-rapidapi-key', Locked = true;
            Header2: Label 'x-rapidapi-host', Locked = true;
        begin
            HttpHeader := HttpClient.DefaultRequestHeaders();
            HttpHeader.Add(Header1, Header1Value);
            HttpHeader.Add(Header2, Header2Value);
    
            if HttpClient.Get(Endpoint, HttpResponse) then
                if HttpResponse.IsSuccessStatusCode() then begin
                    HttpResponse.Content().ReadAs(ResponseText);
                    Message(ResponseText);
                end;
        end;
    
  • garmoogarmoo Member Posts: 21
    Thanks for the help @Resolus :)
Sign In or Register to comment.