Please alternative indy idhttp1.get protocoll SSL run mac osx lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- laguna · 2020-03-15 08:15 Please alternative indy idhttp1.get protocoll SSL run mac osx Thanks ---------------------------------------------------------------------- Remy Lebeau · 2020-03-15 20:59 Quote from: laguna on March 15, 2020, 09:15:53 amPlease alternative indy idhttp1.get protocoll SSL run mac osx Why? Are you having a particular problem with it? ---------------------------------------------------------------------- trev · 2020-03-16 00:08 @lauguna: see this solution that does not require any third party component. ---------------------------------------------------------------------- laguna · 2020-03-17 11:39 Thanks, run with TFPHttpClient in Catalina. How implement this       MPData.AddFile('file', S, 'image/jpg');       MPData.AddFormField('apikey', ApiKey);       MPData.AddFormField('language', lngFromOCR);       MPData.AddFormField('isOverlayRequired', 'False');       sResponse := IdHTTP1.Post(myServerOCR, MPData ,IndyTextEncoding_UTF8); Note:   3 parameter:      MPData.AddFile('file', S, 'image/jpg'); Thanks ---------------------------------------------------------------------- trev · 2020-03-19 22:27 Sorry, I'm not familiar with Indy. I've only used TFPHttpClient for web related tasks. ---------------------------------------------------------------------- Remy Lebeau · 2020-03-20 01:25 Quote from: trev on March 19, 2020, 11:27:24 pmSorry, I'm not familiar with Indy. I've only used TFPHttpClient for web related tasks. laguna is asking how to perform a "multipart/form-data" post with TFPHttpClient, eg: Code: [Select]POST /path HTTP/1.1 Content-Type: multipart/form-data; boundary="boundary" Content-Length: ... --boundary Content-Disposition: form-data; name="file"; filename="image.png" Content-Type: image/jpg --boundary Content-Disposition: form-data; name="apikey" --boundary Content-Disposition: form-data; name="language" --boundary Content-Disposition: form-data; name="isOverlayRequired" False --boundary-- ---------------------------------------------------------------------- Remy Lebeau · 2020-03-20 01:46 Quote from: laguna on March 17, 2020, 12:39:10 pmHow implement this       MPData.AddFile('file', S, 'image/jpg');       MPData.AddFormField('apikey', ApiKey);       MPData.AddFormField('language', lngFromOCR);       MPData.AddFormField('isOverlayRequired', 'False');       sResponse := IdHTTP1.Post(myServerOCR, MPData ,IndyTextEncoding_UTF8); Note:   3 parameter:      MPData.AddFile('file', S, 'image/jpg'); Looking at the source code for TFPHTTPClient, I think it would be something like this: Code: [Select]var   HTTP: TFPHttpClient;   FormData: TStringList;   Resp: TStringStream; ... Resp := TStringStream.Create(''); try   FormData := TStringList.Create;   try     FormData.Add('apikey='+ApiKey);     FormData.Add('language='+lngFromOCR);     FormData.Add('isOverlayRequired=False');     HTTP := TFPHttpClient.Create(nil);     try       // configure HTTP as needed...       HTTP.FileFormPost(myServerOCR, FormData, 'file', S, Resp);     finally       HTTP.Free;     end;   finally     FormData.Free;   end;   sResponse := Resp.DataString; finally   Resp.Free; end; Note, however, that the posted file is always sent as the LAST mime field, and it is always sent with a Content-Type of "application/octet-string" (unlike Indy, which gives you more flexibility over the ordering and media types used).  This MAY or MAY NOT cause issues on the server side, depending on its implementation.  If you need to more closely mimic what Indy sends, you will have to format the MIME data manually, eg (copied from the implementation of FileFormPost() and tweaked): Code: [Select]var   HTTP: TFPHttpClient;   FormData: TStringList;   FS: TFileStream;   Resp: TStringStream;   Tmp, Sep : string;   SS : TRawByteStringStream;   I: Integer;   N, V: String; ... Resp := TStringStream.Create(''); try   FormData := TStringList.Create;   try     FormData.Add('apikey='+ApiKey);     FormData.Add('language='+lngFromOCR);     FormData.Add('isOverlayRequired=False');     Sep := Format('%.8x_multipart_boundary',[Random($ffffff)]);     SS := TRawByteStringStream.Create();     try       Tmp := '--' + Sep + CRLF;       Tmp := Tmp + Format('Content-Disposition: form-data; name="%s"; filename="%s"' + CRLF , ['file', ExtractFileName(S)]);       Tmp := Tmp + 'Content-Type: image/jpg' + CRLF + CRLF;       SS.WriteBuffer(Tmp[1], Length(Tmp));       FS := TFileStream.Create(S, fmOpenRead or fmShareDenyWrite);       try         SS.CopyFrom(FS, FS.Size);       finally         FS.Free;       end;       for I := 0 to FormData.Count -1 do       begin         FormData.GetNameValue(I, N, V);         Tmp := CRLF + '--' + Sep + CRLF;         Tmp := Tmp + Format('Content-Disposition: form-data; name="%s"' + CRLF + CRLF + '%s', [N, V]);         SS.WriteBuffer(Tmp[1], Length(Tmp));       end;       Tmp := CRLF + '--' + Sep + '--' + CRLF;       SS.WriteBuffer(Tmp[1], Length(Tmp));       SS.Position := 0;       HTTP = TFPHttpClient.Create(nil);       try         // configure HTTP as needed...         HTTP.AddHeader('Content-Type', 'multipart/form-data; boundary='+Sep);         HTTP.RequestBody := SS;         HTTP.Post(myServerOCR, Resp);       finally         HTTP.RequestBody := nil;         HTTP.Free;     finally       SS.Free;     end;   finally     FormData.Free;   end;   sResponse := Resp.DataString; finally   Resp.Free; end; ---------------------------------------------------------------------- laguna · 2020-04-10 16:17 I implemented your code, and it gives me this error: see image in this row:   HTTP.AddHeader('Content-Type', 'multipart/form-data; boundary=' + Sep); full code: 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 procedure LeggiOCR2;   var    IdHTTPxx1: TIdHTTP;    MPData: TIdMultiPartFormDataStream;     LD, D: TJSONData;    jItem: TJSONData;    Obj, SubObj: TJSONObject;    Arr: TJSONArray;    sResponse: string;    sValue, S: string;     i: integer;    jData, jData2: TJSONData;     jArray, jArray2: TJSONArray;    Parseur: TJSONParser;    jObject: TJSONObject;    cli: TFPHTTPClient = nil;    myServerOCR: string;    //formdata: TStrings;     HTTP: TFPHttpClient;    FormData: TStringList;    FS: TFileStream;    Resp: TStringStream;    Tmp, Sep: string;    SS: TRawByteStringStream;     N, V: string;   begin     if ServerOCR = 'Server Standard' then      myServerOCR := 'https://api.ocr.space/parse/image';    if ServerOCR = 'Server USA' then      myServerOCR := 'https://apipro1.ocr.space/parse/image';    if ServerOCR = 'Server Europe' then      myServerOCR := 'https://apipro2.ocr.space/parse/image';    if ServerOCR = 'Server Asia' then      myServerOCR := 'https://apipro3.ocr.space/parse/image';     //Resp := TStringStream.Create('');        {$IFDEF DARWIN}    S := opath + '/Contents/Resources/temp.jpg';    //showmessage(s);      {$ELSE}    S := ExtractFilePath(ParamStr(0)) + 'temp.jpg';      {$ENDIF}      Resp := TStringStream.Create('');    try      FormData := TStringList.Create;      try        FormData.Add('apikey=' + ApiKey);        FormData.Add('language=' + lngFromOCR);        FormData.Add('isOverlayRequired=False');         Sep := Format('%.8x_multipart_boundary', [Random($ffffff)]);        HTTP.AddHeader('Content-Type', 'multipart/form-data; boundary=' + Sep);        SS := TRawByteStringStream.Create;        try          Tmp := '--' + Sep + CRLF;          Tmp := Tmp + Format('Content- Disposition: form-data; name="%s"; filename="%s"' +            CRLF, ['file', ExtractFileName(S)]);          Tmp := Tmp + 'Content-Type: image/jpg' + CRLF + CRLF;          SS.WriteBuffer(Tmp[1], Length(Tmp));          ShowMessage(S);          FS := TFileStream.Create(S, fmOpenRead or fmShareDenyWrite);          try          SS.CopyFrom(FS, FS.Size);          finally            FS.Free;          end;          for I := 0 to FormData.Count - 1 do        begin            FormData.GetNameValue(I, N, V);            Tmp := CRLF + '--' + Sep + CRLF;            Tmp := Tmp + Format('Content- Disposition: form-data; name="%s"' +              CRLF + CRLF + '%s', [N, V]);            SS.WriteBuffer(Tmp[1], Length(Tmp));          end;          Tmp := CRLF + '--' + Sep + '--' + CRLF;          SS.WriteBuffer(Tmp[1], Length(Tmp));          SS.Position := 0;        HTTP.RequestBody := SS;          HTTP.Post(myServerOCR, Resp);      finally          HTTP.RequestBody := nil;          SS.Free;        end;      finally        FormData.Free;      end;      sResponse := Resp.DataString;    finally      Resp.Free;    end;      try        jData := GetJSON(sResponse);      parseur := TJSONParser.Create(sResponse);      try        DoParse(parseur);      finally        FreeAndNil(parseur);      end;    finally       //JsonObject.Free;    end;   end; Thanks ---------------------------------------------------------------------- Remy Lebeau · 2020-04-10 20:28 Quote from: laguna on April 10, 2020, 06:17:05 pmI implemented your code, and it gives me this error: see image in this row:   HTTP.AddHeader('Content-Type', 'multipart/form-data; boundary=' + Sep); That is because you are not creating the TFPHttpClient object before using it: 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 HTTP := TFPHttpClient.Create(nil);try  // use HTTP as needed...finally  HTTP.Free;end; I have updated my previous examples to show this.  But that should have been obvious, given the other objects that need to be created before they are used. ---------------------------------------------------------------------- laguna · 2021-01-31 08:29 Hi, After a long time I resumed the project, The program allows you to capture a text type image, perform OCR, and do the translation. for Sierra: https://forum.lazarus.freepascal.org/index.php/topic,41226 .msg285814.html#msg285814 The program was originally developed with Indy components, but it does not work with Catalina. Several users wrote to me if I could update it, I fixed the code you posted to me, but I don't get the result. URL Api https://ocr.space/ocrapi with sampler I'll post the code. I have made the procedure autonomous. I also entered the API string, You just need to update the Path of the JPG image 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 procedure LeggiOCR;   var    sResponse: string;     S: string;     i: integer;   myServerOCR: string;      HTTP: TFPHttpClient;    FormData: TStringList;    FS: TFileStream;    Resp: TStringStream;    Tmp, Sep: string;    SS: TRawByteStringStream;     N, V: string;   begin     myServerOCR := 'https://api.ocr.space/parse/image';      ApiKey :='';     s:='/Users/enzo/Desktop/test.jpg';     Resp := TStringStream.Create('');    try      HTTP := TFPHttpClient.Create(nil);      FormData := TStringList.Create;      try        FormData.Add('apikey=' + ApiKey);        FormData.Add('language=' + 'EN');        FormData.Add('isOverlayRequired=False');         Sep := Format('%.8x_multipart_boundary', [Random($ffffff)]);        HTTP.AddHeader('Content-Type', 'multipart/form-data; boundary=' + Sep);        SS := TRawByteStringStream.Create;        try          Tmp := '--' + Sep + CRLF;          Tmp := Tmp + Format('Content- Disposition: form-data; name="%s"; filename="%s"' +            CRLF, ['file', ExtractFileName(S)]);          Tmp := Tmp + 'Content-Type: image/jpg' + CRLF + CRLF;          SS.WriteBuffer(Tmp[1], Length(Tmp));           FS := TFileStream.Create(S, fmOpenRead or fmShareDenyWrite);          try            SS.CopyFrom(FS, FS.Size);        finally            FS.Free;          end;          for I := 0 to FormData.Count - 1 do          begin            FormData.GetNameValue(I, N, V);            Tmp := CRLF + '--' + Sep + CRLF;            Tmp := Tmp + Format('Content-Disposition: form-data; name="%s"' +              CRLF + CRLF + '%s', [N, V]);            SS.WriteBuffer(Tmp[1], Length(Tmp));          end;          Tmp := CRLF + '--' + Sep + '--' + CRLF;          SS.WriteBuffer(Tmp[1], Length(Tmp));          SS.Position := 0;          HTTP.RequestBody := SS;          HTTP.Post(myServerOCR, Resp);         finally          HTTP.RequestBody := nil;          SS.Free;        end;      finally      FormData.Free;      end;      sResponse := Resp.DataString;      ShowMessage(sResponse);    finally      Resp.Free;    end;   end; I report the Indy function that worked with sierra 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     MPData := TIdMultiPartFormDataStream.Create;      MPData.AddFile('file', S, 'image/jpg');       MPData.AddFormField('apikey', ApiKey);      MPData.AddFormField('language', lngFromOCR);      MPData.AddFormField('isOverlayRequired', 'False');      // IdHTTPxx1.Request.CharSet := 'utf-8';     try     // showmessage(PostData);      IdHTTPxx1 := TIdHTTP.Create(nil);      IdHTTPxx1.Request.ContentType := 'application/x-www-form- urlencoded';       sResponse := IdHTTPxx1.Post(myServerOCR, MPData ,IndyTextEncoding_UTF8); Thanks all ---------------------------------------------------------------------- laguna · 2021-01-31 10:23 Run. after cleaning the code, it works. Thank you all. ---------------------------------------------------------------------- Remy Lebeau · 2021-01-31 19:57 Quote from: laguna on January 31, 2021, 09:29:59 amThe program was originally developed with Indy components, but it does not work with Catalina. But, you never explained WHY it doesn't work.  What is the actual problem with it? ---------------------------------------------------------------------- laguna · 2021-02-03 13:22 The OCR server was down. And I haven't noticed or not working