Recent

Author Topic: [solved] how to post a post-request  (Read 650 times)

Nicole

  • Hero Member
  • *****
  • Posts: 1182
[solved] how to post a post-request
« on: May 20, 2025, 07:59:58 pm »
I am newbie in web-programming and API.
After many attempts and errors I was able to do some get requests, which even work.
Now I tried a post-request and I failed.

Please do not wonder, it is not my code, but I asked chatGPT for it.
It tried so many times and versions, - it never would work without a human.
Read the comment of ChatGPT,.... it cannot even count up to three.  :-\

What does not work is the crucial thing of it:
Sending the post and saving the answer.

What I want to do under the line is this:
https://www.openfigi.com/api/documentation

This is what it wrote

Code: Pascal  [Select][+][-]
  1. //uses
  2.  // fphttpclient, fpjson, jsonparser, sysutils, classes;
  3.  
  4. function GetFigiInfo(const Ticker: string): string;
  5. var
  6.   HttpClient: TFPHTTPClient;
  7.   ResponseStream: TStringStream;
  8.   BodyStream: TStringStream;
  9.   ResponseText: string;
  10.   Json, FigiData: TJSONData;
  11.   Url, Body, API_KEY: string;
  12. begin
  13.   SetLength(Result, 0);
  14.   HttpClient := TFPHTTPClient.Create(nil);
  15.   API_KEY := 'Dein_API_Schlüssel';  
  16.   Url := 'https://api.openfigi.com/v3/mapping';
  17.   Body := '{"query":[{"ticker":"' + Ticker + '","exchCode":"XNYS"}]}';
  18.  
  19.   ResponseStream := TStringStream.Create('');
  20.   BodyStream := TStringStream.Create(Body);
  21.  
  22.   HttpClient.AddHeader('Content-Type', 'application/json');
  23.   HttpClient.AddHeader('X-OPENFIGI-APIKEY', API_KEY);
  24.  
  25.   // Richtiges Post mit 4 Parametern: URL, BodyStream (Source), ResponseStream (Dest)
  26.   HttpClient.Post(Url, BodyStream, ResponseStream);
  27.  
  28.   ResponseText := ResponseStream.DataString;
  29.   Json := GetJSON(ResponseText);
  30.   FigiData := Json.FindPath('data');
  31.   if Assigned(FigiData) then
  32.     Result := FigiData.AsJSON
  33.   else
  34.     Result := 'Keine Daten gefunden.';
  35.  
  36.   ResponseStream.Free;
  37.   BodyStream.Free;
  38.   HttpClient.Free;
  39. end;
« Last Edit: May 24, 2025, 12:13:04 pm by Nicole »

bytebites

  • Hero Member
  • *****
  • Posts: 717
Re: how to post a post-request
« Reply #1 on: May 20, 2025, 09:54:47 pm »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1530
    • Lebeau Software
Re: how to post a post-request
« Reply #2 on: May 20, 2025, 09:55:43 pm »
What does not work is the crucial thing of it:
Sending the post and saving the answer.

The JSON you are sending is completely wrong for the URL you are sending it to.  Apparently you did not read the documentation you linked to.

You are sending a single JSON object containing a "query" field to the '/v3/mapping' URL, but that JSON is meant for the '/v3/search' URL instead.  The '/v3/mapping' URL wants an array of JSON objects, each containing "idType", "idValue", etc fields.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Nicole

  • Hero Member
  • *****
  • Posts: 1182
Re: how to post a post-request
« Reply #3 on: May 24, 2025, 12:12:45 pm »
Thank you for the answers!

Here is my working example, call it e.g. by "s:=GetFigiInfo('SAP');"

Code: Pascal  [Select][+][-]
  1. function GetFigiInfo(const Ticker: string): string;
  2. const
  3.   API_URL = 'https://api.openfigi.com/v2/mapping';
  4.   API_KEY = 'xxxxxxxxxxxxxxx';  // please get a free key by visiting the domain
  5.  
  6. var
  7.   HttpClient: TFPHttpClient;
  8.   Response: TStringStream;
  9.   RequestBody: TStringStream;
  10.   JsonRequest: string;
  11.   Exchange: string;
  12. begin
  13.   Result := '';
  14.   JsonRequest := '[{"idType":"TICKER","idValue":"' + Ticker + '"}]';
  15.  
  16.   HttpClient := TFPHttpClient.Create(nil);
  17.   Response := TStringStream.Create('');
  18.   RequestBody := TStringStream.Create(JsonRequest);
  19.   try
  20.     // Header setzen
  21.     HttpClient.AddHeader('Content-Type', 'application/json; charset=UTF-8');
  22.     HttpClient.AddHeader('X-OPENFIGI-APIKEY', API_KEY);
  23.  
  24.     // JSON-Body zuweisen und POST senden
  25.     HttpClient.RequestBody := RequestBody;
  26.     HttpClient.Post(API_URL, Response);
  27.  
  28.     // Antwort als String zurückgeben
  29.     Result := Response.DataString;
  30.   finally
  31.     Response.Free;
  32.     RequestBody.Free;
  33.     HttpClient.Free;
  34.   end;
  35. end;  
         
« Last Edit: May 24, 2025, 12:44:04 pm by Nicole »

 

TinyPortal © 2005-2018