* * *

Author Topic: [Solved using Indy] Replicate CURL command with FPHTTPClient  (Read 2594 times)

lainz

  • Hero Member
  • *****
  • Posts: 2828
    • Home
[Solved using Indy] Replicate CURL command with FPHTTPClient
« on: April 19, 2018, 07:48:33 pm »
Hi, I finally get CURL to do his job, is a fiscal printer emulator that receives the command

Code: Pascal  [Select]
  1. curl --noproxy 192.168.56.1 http://192.168.56.1/fiscal.json -H "Content-Type: application/json" --data-binary @%1 > resp.json

@%1:
Code: Pascal  [Select]
  1. {"ConsultarVersion":{}}

This is my code, that doesn't works:

Code: Pascal  [Select]
  1. var
  2.   client: TFPHTTPClient;
  3.   ss: TMemoryStream;
  4.   sJSON: TJSONStringType;
  5.   data: TJSONObject;
  6. begin
  7.   try
  8.     client := TFPHttpClient.Create(nil);
  9.     client.AddHeader('Content-Type', 'application/json');
  10.     client.AddHeader('Accept', 'application/json');
  11.     data := TJSONObject.Create;
  12.     data.Add('ConsultarVersion', TJSONObject.Create);
  13.     sJSON := data.AsJSON;
  14.     data.Free;
  15.     ss := TMemoryStream.Create();
  16.     ss.Write(Pointer(sJSON)^, length(sJSON));
  17.     ss.Position := 0;
  18.     client.RequestBody := ss;
  19.     Result := client.Post('http://192.168.56.1/fiscal.json');
  20.   finally
  21.     ss.Free;
  22.     client.Free;
  23.   end;

I'm not sure if is that the fiscal printer needs a file.. instead of JSON in the body.
« Last Edit: April 19, 2018, 09:27:28 pm by lainz »

Phil

  • Hero Member
  • *****
  • Posts: 2750
Re: Replicate CURL command with FPHTTPClient
« Reply #1 on: April 19, 2018, 08:06:30 pm »
I'm not sure if is that the fiscal printer needs a file.. instead of JSON in the body.

I think something like this should work:

Result := client.SimpleFormPost('http://192.168.56.1', ' {"ConsultarVersion":{}}');

Note the JSON needs to be encoded (not an issue here).


GetMem

  • Hero Member
  • *****
  • Posts: 3187
Re: Replicate CURL command with FPHTTPClient
« Reply #2 on: April 19, 2018, 08:12:47 pm »
I also think SimpleFormPost should work, something like this: http://forum.lazarus.freepascal.org/index.php/topic,39638.msg272761.html#msg272761

lainz

  • Hero Member
  • *****
  • Posts: 2828
    • Home
Re: Replicate CURL command with FPHTTPClient
« Reply #3 on: April 19, 2018, 08:16:42 pm »
Hi @Phil, @Getmem, I try with your code, but the same with my code, it always return an empty string and StatusCode 0
Code: Pascal  [Select]
  1. ShowMessage(client.ResponseStatusCode.ToString); // 0
  2.     ShowMessage(client.ResponseHeaders.Text); // empty
  3.     ShowMessage(client.ResponseStatusText);// empty
  4.     ShowMessage(Result); // empty

GetMem

  • Hero Member
  • *****
  • Posts: 3187
Re: Replicate CURL command with FPHTTPClient
« Reply #4 on: April 19, 2018, 08:20:47 pm »
Try to add:
Code: Pascal  [Select]
  1. Client.AllowRedirect := True;

Just to be sure: you need to post a json file and the server will return a json file? Or you simple need to download a json?
« Last Edit: April 19, 2018, 08:24:35 pm by GetMem »

lainz

  • Hero Member
  • *****
  • Posts: 2828
    • Home
Re: Replicate CURL command with FPHTTPClient
« Reply #5 on: April 19, 2018, 08:25:53 pm »
No luck.

Is a website like the router one, where you can see the settings, and data. And there is an 'endpoint' that is http://192.168.56.1/fiscal.json

And I tested with Postman, and it works. So there is something wrong with my lazarus maybe? I will try with 1.8, not trunk.

In Postman, I set the radio button 'Raw'.

lainz

  • Hero Member
  • *****
  • Posts: 2828
    • Home
Re: Replicate CURL command with FPHTTPClient
« Reply #6 on: April 19, 2018, 08:27:59 pm »
Try to add:
Code: Pascal  [Select]
  1. Client.AllowRedirect := True;

Just to be sure: you need to post a json file and the server will return a json file? Or you simple need to download a json?

Yes, I post the JSON and a JSON is returned.

Edit: No luck also with Lazarus 1.8

Info in Postman:

Code: Pascal  [Select]
  1. Access-Control-Allow-Methods →OPTIONS, POST, GET
  2. Access-Control-Allow-Origin →*
  3. Access-Control-Max-Age →86400
  4. Connection →close
  5. Content-Type →application/json; charset=UTF-8
  6. Date →Thu, 19 Apr 2018 18:33:39 GMT
  7. Server →HSL HTTP Server

HSL?
« Last Edit: April 19, 2018, 08:34:38 pm by lainz »

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: Replicate CURL command with FPHTTPClient
« Reply #7 on: April 19, 2018, 08:45:05 pm »

lainz

  • Hero Member
  • *****
  • Posts: 2828
    • Home
Re: Replicate CURL command with FPHTTPClient
« Reply #8 on: April 19, 2018, 08:49:09 pm »
HSL?
Perhaps you meant HLS ?

No, I just copy/paste it. And searching for HSL there is no meaningfull information, maybe it's a 'bug' in text of the emulator..

GetMem

  • Hero Member
  • *****
  • Posts: 3187
Re: Replicate CURL command with FPHTTPClient
« Reply #9 on: April 19, 2018, 09:03:43 pm »
@lainz

As a last try:
Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   FPHTTPClient: TFPHTTPClient;
  4.   SL: TStringList;
  5.   Str: String;
  6. begin
  7.   FPHTTPClient := TFPHTTPClient.Create(nil);
  8.   try
  9.     FPHTTPClient.AllowRedirect := True;
  10.     FPHTTPClient.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)');
  11.     FPHTTPClient.AddHeader('Content-Type', 'application/json');
  12.     FPHTTPClient.AddHeader('Accept', 'application/json');
  13.     SL := TStringList.Create;
  14.     try
  15.       SL.Add('{"ConsultarVersion":{}}');
  16.       try
  17.         Str := FPHTTPClient.SimpleFormPost('http://192.168.56.1/fiscal.json', SL);
  18.         ShowMessage(Str);
  19.       except
  20.         on E: exception do
  21.           ShowMessage(E.Message);
  22.       end;
  23.     finally
  24.       SL.Free;
  25.     end;
  26.   finally
  27.     FPHTTPClient.Free;
  28.   end;
  29. end;            

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: Replicate CURL command with FPHTTPClient
« Reply #10 on: April 19, 2018, 09:09:25 pm »
HSL?
Perhaps you meant HLS ?

No, I just copy/paste it. And searching for HSL there is no meaningfull information, maybe it's a 'bug' in text of the emulator..
Ok, just trying  :)

I was also unable to find anything useful with regard to HSL (well, in relation with http that is)

lainz

  • Hero Member
  • *****
  • Posts: 2828
    • Home
Re: Replicate CURL command with FPHTTPClient
« Reply #11 on: April 19, 2018, 09:10:24 pm »
Thanks @GetMem, but I always get an empty string.

I found online that the CURL command is this:

Code: Pascal  [Select]
  1. --data-binary DATA  HTTP POST binary data (H)

So it should be sent as binary, no matter if it's application/json.. I don't know. But in Postman the only option that works is the 'raw' one. Using form-data, x-www-form-urlencoded or binary does not works.

So is binary or not  %)


HSL?
Perhaps you meant HLS ?

No, I just copy/paste it. And searching for HSL there is no meaningfull information, maybe it's a 'bug' in text of the emulator..
Ok, just trying  :)

I was also unable to find anything useful with regard to HSL (well, in relation with http that is)

Thanks molly =)
But I get permission to use Indy for now, I will see if it works with Indy.

lainz

  • Hero Member
  • *****
  • Posts: 2828
    • Home
Re: Replicate CURL command with FPHTTPClient
« Reply #12 on: April 19, 2018, 09:27:04 pm »
Solved  :)

Indy works fine

Code: Pascal  [Select]
  1. var
  2.   ReqJson: TStringStream;
  3. begin
  4.   ReqJson := TStringStream.Create('{"ConsultarVersion":{}}', TEncoding.UTF8);
  5.   try
  6.     IdHTTP1.Request.ContentType := 'application/json';
  7.     ShowMessage(IdHTTP1.Post('http://192.168.56.1/fiscal.json', ReqJson));
  8.   finally
  9.     ReqJson.Free;
  10.   end;

I get a JSON with the right data.

Code: Javascript  [Select]
  1. {
  2.         "ConsultarVersion":
  3.         {
  4.                 "Secuencia": "43",
  5.                 "Estado":
  6.                 {
  7.                         "Impresora":
  8.                         [
  9.                         ],
  10.                         "Fiscal":
  11.                         [
  12.                                 "MemoriaFiscalInicializada"
  13.                         ]
  14.                 },
  15.                 "Version": "SMH/PT-1000F V: 01.00",
  16.                 "Marca": "Hasar",
  17.                 "NombreProducto": "CF",
  18.                 "VersionMotor": "6.02A",
  19.                 "FechaFirmware": "17/02/2017 11:10:50",
  20.                 "VersionProtocolo": "0"
  21.         }
  22. }


Thanks to all, I mark it as solved.
« Last Edit: April 19, 2018, 09:29:12 pm by lainz »

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: [Solved using Indy] Replicate CURL command with FPHTTPClient
« Reply #13 on: April 19, 2018, 10:12:49 pm »
@lainz:
Now that you have a working setup, perhaps you can be bothered to figure out what is the difference between TFPHTTPClient and indy. You could use a service like httpbin (or similar) for that.

lainz

  • Hero Member
  • *****
  • Posts: 2828
    • Home
Re: [Solved using Indy] Replicate CURL command with FPHTTPClient
« Reply #14 on: April 19, 2018, 10:21:53 pm »
@lainz:
Now that you have a working setup, perhaps you can be bothered to figure out what is the difference between TFPHTTPClient and indy. You could use a service like httpbin (or similar) for that.

Sure. I will do it to see if fphttpclient can be used as well.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus