Recent

Author Topic: Please alternative indy idhttp1.get protocoll SSL run mac osx  (Read 2751 times)

laguna

  • Sr. Member
  • ****
  • Posts: 323
Please alternative indy idhttp1.get protocoll SSL run mac osx

Thanks

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Please alternative indy idhttp1.get protocoll SSL run mac osx
« Reply #1 on: March 15, 2020, 09:59:48 pm »
Please alternative indy idhttp1.get protocoll SSL run mac osx

Why? Are you having a particular problem with it?
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2023
  • Former Delphi 1-7, 10.2 user
Re: Please alternative indy idhttp1.get protocoll SSL run mac osx
« Reply #2 on: March 16, 2020, 01:08:42 am »
@lauguna: see this solution that does not require any third party component.

laguna

  • Sr. Member
  • ****
  • Posts: 323
Re: Please alternative indy idhttp1.get protocoll SSL run mac osx
« Reply #3 on: March 17, 2020, 12:39:10 pm »
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2023
  • Former Delphi 1-7, 10.2 user
Re: Please alternative indy idhttp1.get protocoll SSL run mac osx
« Reply #4 on: March 19, 2020, 11:27:24 pm »
Sorry, I'm not familiar with Indy. I've only used TFPHttpClient for web related tasks.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Please alternative indy idhttp1.get protocoll SSL run mac osx
« Reply #5 on: March 20, 2020, 02:25:02 am »
Sorry, 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

<png file data here>
--boundary
Content-Disposition: form-data; name="apikey"

<ApiKey here>
--boundary
Content-Disposition: form-data; name="language"

<lngFromOCR here>
--boundary
Content-Disposition: form-data; name="isOverlayRequired"

False
--boundary--
« Last Edit: March 20, 2020, 02:30:38 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Please alternative indy idhttp1.get protocoll SSL run mac osx
« Reply #6 on: March 20, 2020, 02:46:06 am »
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');

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;
« Last Edit: April 10, 2020, 10:31:52 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

laguna

  • Sr. Member
  • ****
  • Posts: 323
Re: Please alternative indy idhttp1.get protocoll SSL run mac osx
« Reply #7 on: April 10, 2020, 06:17:05 pm »
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][+][-]
  1. procedure LeggiOCR2;
  2.  
  3.   var
  4.     IdHTTPxx1: TIdHTTP;
  5.     MPData: TIdMultiPartFormDataStream;
  6.  
  7.     LD, D: TJSONData;
  8.     jItem: TJSONData;
  9.     Obj, SubObj: TJSONObject;
  10.     Arr: TJSONArray;
  11.     sResponse: string;
  12.     sValue, S: string;
  13.  
  14.     i: integer;
  15.     jData, jData2: TJSONData;
  16.  
  17.     jArray, jArray2: TJSONArray;
  18.     Parseur: TJSONParser;
  19.     jObject: TJSONObject;
  20.     cli: TFPHTTPClient = nil;
  21.     myServerOCR: string;
  22.     //formdata: TStrings;
  23.  
  24.     HTTP: TFPHttpClient;
  25.     FormData: TStringList;
  26.     FS: TFileStream;
  27.     Resp: TStringStream;
  28.     Tmp, Sep: string;
  29.     SS: TRawByteStringStream;
  30.  
  31.     N, V: string;
  32.  
  33.   begin
  34.  
  35.     if ServerOCR = 'Server Standard' then
  36.       myServerOCR := 'https://api.ocr.space/parse/image';
  37.     if ServerOCR = 'Server USA' then
  38.       myServerOCR := 'https://apipro1.ocr.space/parse/image';
  39.     if ServerOCR = 'Server Europe' then
  40.       myServerOCR := 'https://apipro2.ocr.space/parse/image';
  41.     if ServerOCR = 'Server Asia' then
  42.       myServerOCR := 'https://apipro3.ocr.space/parse/image';
  43.  
  44.     //Resp := TStringStream.Create('');
  45.  
  46.  
  47.       {$IFDEF DARWIN}
  48.     S := opath + '/Contents/Resources/temp.jpg';
  49.     //showmessage(s);
  50.       {$ELSE}
  51.     S := ExtractFilePath(ParamStr(0)) + 'temp.jpg';
  52.       {$ENDIF}
  53.  
  54.  
  55.     Resp := TStringStream.Create('');
  56.     try
  57.       FormData := TStringList.Create;
  58.       try
  59.         FormData.Add('apikey=' + ApiKey);
  60.         FormData.Add('language=' + lngFromOCR);
  61.         FormData.Add('isOverlayRequired=False');
  62.  
  63.         Sep := Format('%.8x_multipart_boundary', [Random($ffffff)]);
  64.         HTTP.AddHeader('Content-Type', 'multipart/form-data; boundary=' + Sep);
  65.         SS := TRawByteStringStream.Create;
  66.         try
  67.           Tmp := '--' + Sep + CRLF;
  68.           Tmp := Tmp + Format('Content-Disposition: form-data; name="%s"; filename="%s"' +
  69.             CRLF, ['file', ExtractFileName(S)]);
  70.           Tmp := Tmp + 'Content-Type: image/jpg' + CRLF + CRLF;
  71.           SS.WriteBuffer(Tmp[1], Length(Tmp));
  72.           ShowMessage(S);
  73.           FS := TFileStream.Create(S, fmOpenRead or fmShareDenyWrite);
  74.           try
  75.             SS.CopyFrom(FS, FS.Size);
  76.           finally
  77.             FS.Free;
  78.           end;
  79.           for I := 0 to FormData.Count - 1 do
  80.           begin
  81.             FormData.GetNameValue(I, N, V);
  82.             Tmp := CRLF + '--' + Sep + CRLF;
  83.             Tmp := Tmp + Format('Content-Disposition: form-data; name="%s"' +
  84.               CRLF + CRLF + '%s', [N, V]);
  85.             SS.WriteBuffer(Tmp[1], Length(Tmp));
  86.           end;
  87.           Tmp := CRLF + '--' + Sep + '--' + CRLF;
  88.           SS.WriteBuffer(Tmp[1], Length(Tmp));
  89.           SS.Position := 0;
  90.           HTTP.RequestBody := SS;
  91.           HTTP.Post(myServerOCR, Resp);
  92.         finally
  93.           HTTP.RequestBody := nil;
  94.           SS.Free;
  95.         end;
  96.       finally
  97.         FormData.Free;
  98.       end;
  99.       sResponse := Resp.DataString;
  100.     finally
  101.       Resp.Free;
  102.     end;
  103.  
  104.  
  105.     try
  106.  
  107.  
  108.       jData := GetJSON(sResponse);
  109.       parseur := TJSONParser.Create(sResponse);
  110.       try
  111.         DoParse(parseur);
  112.       finally
  113.         FreeAndNil(parseur);
  114.       end;
  115.     finally
  116.  
  117.       //JsonObject.Free;
  118.     end;
  119.  
  120.   end;  

Thanks



Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Please alternative indy idhttp1.get protocoll SSL run mac osx
« Reply #8 on: April 10, 2020, 10:28:19 pm »
I 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][+][-]
  1. HTTP := TFPHttpClient.Create(nil);
  2. try
  3.   // use HTTP as needed...
  4. finally
  5.   HTTP.Free;
  6. 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.
« Last Edit: April 10, 2020, 10:32:54 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

laguna

  • Sr. Member
  • ****
  • Posts: 323
Re: Please alternative indy idhttp1.get protocoll SSL run mac osx
« Reply #9 on: January 31, 2021, 09:29:59 am »
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][+][-]
  1. procedure LeggiOCR;
  2.  
  3.   var
  4.     sResponse: string;
  5.      S: string;
  6.      i: integer;
  7.      myServerOCR: string;
  8.  
  9.      HTTP: TFPHttpClient;
  10.     FormData: TStringList;
  11.     FS: TFileStream;
  12.     Resp: TStringStream;
  13.     Tmp, Sep: string;
  14.     SS: TRawByteStringStream;
  15.  
  16.     N, V: string;
  17.  
  18.   begin
  19.  
  20.  
  21.    myServerOCR := 'https://api.ocr.space/parse/image';
  22.  
  23.  
  24.     ApiKey :='';
  25.  
  26.     s:='/Users/enzo/Desktop/test.jpg';
  27.  
  28.     Resp := TStringStream.Create('');
  29.     try
  30.       HTTP := TFPHttpClient.Create(nil);
  31.       FormData := TStringList.Create;
  32.       try
  33.         FormData.Add('apikey=' + ApiKey);
  34.         FormData.Add('language=' + 'EN');
  35.         FormData.Add('isOverlayRequired=False');
  36.  
  37.         Sep := Format('%.8x_multipart_boundary', [Random($ffffff)]);
  38.         HTTP.AddHeader('Content-Type', 'multipart/form-data; boundary=' + Sep);
  39.         SS := TRawByteStringStream.Create;
  40.         try
  41.           Tmp := '--' + Sep + CRLF;
  42.           Tmp := Tmp + Format('Content-Disposition: form-data; name="%s"; filename="%s"' +
  43.             CRLF, ['file', ExtractFileName(S)]);
  44.           Tmp := Tmp + 'Content-Type: image/jpg' + CRLF + CRLF;
  45.           SS.WriteBuffer(Tmp[1], Length(Tmp));
  46.  
  47.           FS := TFileStream.Create(S, fmOpenRead or fmShareDenyWrite);
  48.           try
  49.             SS.CopyFrom(FS, FS.Size);
  50.           finally
  51.             FS.Free;
  52.           end;
  53.           for I := 0 to FormData.Count - 1 do
  54.           begin
  55.             FormData.GetNameValue(I, N, V);
  56.             Tmp := CRLF + '--' + Sep + CRLF;
  57.             Tmp := Tmp + Format('Content-Disposition: form-data; name="%s"' +
  58.               CRLF + CRLF + '%s', [N, V]);
  59.             SS.WriteBuffer(Tmp[1], Length(Tmp));
  60.           end;
  61.           Tmp := CRLF + '--' + Sep + '--' + CRLF;
  62.           SS.WriteBuffer(Tmp[1], Length(Tmp));
  63.           SS.Position := 0;
  64.           HTTP.RequestBody := SS;
  65.           HTTP.Post(myServerOCR, Resp);
  66.  
  67.         finally
  68.           HTTP.RequestBody := nil;
  69.           SS.Free;
  70.         end;
  71.       finally
  72.         FormData.Free;
  73.       end;
  74.       sResponse := Resp.DataString;
  75.       ShowMessage(sResponse);
  76.     finally
  77.       Resp.Free;
  78.     end;
  79.  
  80.   end;  
  81.  


I report the Indy function that worked with sierra


Code: Pascal  [Select][+][-]
  1.     MPData := TIdMultiPartFormDataStream.Create;
  2.       MPData.AddFile('file', S, 'image/jpg');
  3.  
  4.       MPData.AddFormField('apikey', ApiKey);
  5.       MPData.AddFormField('language', lngFromOCR);
  6.       MPData.AddFormField('isOverlayRequired', 'False');
  7.       // IdHTTPxx1.Request.CharSet := 'utf-8';
  8.  
  9.     try
  10.      // showmessage(PostData);
  11.       IdHTTPxx1 := TIdHTTP.Create(nil);
  12.       IdHTTPxx1.Request.ContentType := 'application/x-www-form-urlencoded';
  13.  
  14.       sResponse := IdHTTPxx1.Post(myServerOCR, MPData ,IndyTextEncoding_UTF8);
  15.  


Thanks all

« Last Edit: January 31, 2021, 10:23:57 am by laguna »

laguna

  • Sr. Member
  • ****
  • Posts: 323
Re: Please alternative indy idhttp1.get protocoll SSL run mac osx
« Reply #10 on: January 31, 2021, 11:23:53 am »
Run.

after cleaning the code, it works.

Thank you all.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Please alternative indy idhttp1.get protocoll SSL run mac osx
« Reply #11 on: January 31, 2021, 08:57:14 pm »
The 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?
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

laguna

  • Sr. Member
  • ****
  • Posts: 323
Re: Please alternative indy idhttp1.get protocoll SSL run mac osx
« Reply #12 on: February 03, 2021, 02:22:51 pm »
The OCR server was down.
And I haven't noticed or not working

 

TinyPortal © 2005-2018