Trying to set up a codeunit to consume External REST API

garmoo
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
but I am getting this error

Has anyone any idea of what I'm doing wrong? Or any links to good blogs that explains the process in simple terms?
Thanks!
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

Has anyone any idea of what I'm doing wrong? Or any links to good blogs that explains the process in simple terms?
Thanks!
0
Best 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;
0
Answers
-
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;
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions