LibreTranslate lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- SymbolicFrank · 2023-01-24 14:51 Ok, I just spend a few hours trying to communicate with this. It seems easy enough: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TMainForm.Translate(s: string): string;var  client: TFPHttpClient;  response: TStringStream;  url, r: string;begin  client := TFPHttpClient.Create(nil);  response := TStringStream.Create('');  url := 'https://libretranslate.com/translate';  r := '{q: "' + s +    '", source: "nl", target: "en", format: "text", api_key: ""}';  client.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)');  client.AddHeader('Content-Type', 'application/json');  client.AddHeader('Accept', 'application/json');  client.AllowRedirect := true;  client.RequestBody := TRawByteStringStream.Create(r);  client.Post(url, response);  Result := response.DataString;  client.Free;  response.Free;end; Ok, I use: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i url := 'http://localhost:5000/translate'; I tried many variations, but none of them work. I always get a 400: BAD REQUEST; '{"error":"The browser (or proxy) sent a request that this server could not understand."}' It is probably really easy, but I don't get it. ---------------------------------------------------------------------- arneolav · 2023-01-24 14:54 Have a look here, did help me: https://wiki.freepascal.org/fphttpclient Look for this: program dl_fphttp_d; "it demonstrates almost everything" ---------------------------------------------------------------------- PierceNg · 2023-01-25 06:10 Quote from: SymbolicFrank on January 24, 2023, 03:51:37 pmOk, I just spend a few hours trying to communicate with this. It seems easy enough: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TMainForm.Translate(s: string): string;  r := '{q: "' + s +    '", source: "nl", target: "en", format: "text", api_key: ""}'; I tried many variations, but none of them work. I always get a 400: BAD REQUEST; '{"error":"The browser (or proxy) sent a request that this server could not understand."}' Try double quoting the JSON keys. When I do JSON.stringify in Javascript and print it out, the keys are quoted: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i '{"q":"","source":"auto","target":"en","format":"text","api_key":""}' ---------------------------------------------------------------------- SymbolicFrank · 2023-01-25 07:54 Quote from: PierceNg on January 25, 2023, 07:10:23 amWhen I do JSON.stringify in Javascript and print it out, the keys are quoted: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i '{"q":"","source":"auto","target":"en","format":"text","api_key":""}' Thanks, that was it! Obvious, in hindsight. ---------------------------------------------------------------------- wp · 2023-01-25 09:16 It still is not working for me: "The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application." And there is a memory leak because a stream is created for client.RequestBody which is not destroyed. [EDIT] It is working when I move to a different url (https://github.com/argosopentech/argos-translate): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   url := 'https://translate.argosopentech.com/translate'; Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  ..., fphttpclient, opensslsockets, fpjson, jsonparser;  function Translate(AText, SrcLang, DestLang: string): string;var  client: TFPHttpClient;  request: TRawByteStringStream;  response: TMemoryStream;  url, r: string;  json: TJSONData;begin  Result := '';   url := 'https://translate.argosopentech.com/translate';  r := Format('{"q":"%s", "source":"%s", "target":"%s", "format":"text"}', [    AText, SrcLang, DestLang  ]);   client := TFPHttpClient.Create(nil);  try    client.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)');    client.AddHeader('Content- Type', 'application/json');    client.AddHeader('Accept', 'application/json');    client.AllowRedirect := true;    request := TRawByteStringStream.Create(r);    response := TMemoryStream.Create;  try      client.RequestBody := request;      client.Post(url, response);      response.Position := 0;      json := GetJSON(response);      try        if json <> nil then          Result := json.FindPath('translatedText').AsString;      finally        json.Free;      end;    finally      response.Free;      request.Free;    end;  finally    client.Free;  end;end;