Forum > General
openAPI client
rnervi:
sure, it's json but didnt understood how to write dowb simple code to call and use answers. They supply some code snippet for many langs BUT object pascal.
meanwhile I asked them if any tutorial or code snippet natively.
then I change - simpyfing - question:
is there a basic delphi code to call an api service and get answer (to be post processed) ?
paweld:
Example of get data from endpoint IT-legalforms of company module. You need to generate a token in their panels or using the API.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses fphttpclient, opensslsockets, fpjson; const api_url = 'https://test.company.openapi.com/'; // for COMPANY module: <-- sandbox | production --> 'https://company.openapi.com/' YOUR_OpenAPI_Token = 'sample-OpenAPI-fake-token'; //paste your token procedure TForm1.Button1Click(Sender: TObject);var hc: TFPHttpClient; errmsg: String; jsonstream: TStringStream; jo: TJsonObject;begin errmsg := ''; hc := TFPHttpClient.Create(nil); hc.AllowRedirect := True; hc.RequestHeaders.Insert(0, 'Authorization: Bearer ' + YOUR_OpenAPI_Token); hc.RequestHeaders.Add('Content-Type: application/json'); jsonstream := TStringStream.Create; hc.HTTPMethod('GET', api_url + 'IT-legalforms', jsonstream, [200, 202, 400, 401, 402, 403]); //sample endpoint if hc.ResponseStatusCode div 100 = 2 then //if response OK Memo1.Lines.Add(jsonstream.DataString) //here I add response to TMemo, but you can process and get the information you need, just as I did below else begin if jsonstream.DataString <> '' then begin jo := TJsonObject(GetJson(jsonstream.DataString)); if jo.Find('message') <> nil then errmsg := jo.Find('message').AsString; jo.Free; end; ShowMessage(Format('Error: [%d] %s' + #13#10 + 'OpenAPI Error Message: %s', [hc.ResponseStatusCode, hc.ResponseStatusText, errmsg])); end; jsonstream.Free; hc.Free;end;
rnervi:
oh ! many thanks
will analyze and test it
yes I know for the token from cpanel
ty
rnervi:
for the correctness
must copy libeay32.dll and ssleay32.dll inside the dir for exe file, to avoid "cannot initialize ssl library"
and .. it works !
ty a lot (spent last 2 days in figure howto!)
gidesa:
There is this library, for FPC and Delphi:
https://github.com/landgraf-dev/openai-delphi
Navigation
[0] Message Index
[#] Next page
[*] Previous page