Recent

Author Topic: [Solved] Tprocess and Curl  (Read 542 times)

chrnobel

  • Sr. Member
  • ****
  • Posts: 261
[Solved] Tprocess and Curl
« on: July 02, 2019, 12:41:23 am »
Hi.

I am trying to retrieve some REST data via CURL.

The command line CURL sentence is:

curl -X GET -H "X-AppSecretToken:demo" -H "X-AgreementGrantToken:demo" -H "Content-Type: application/json" https://restapi.e-conomic.com/products

But if I try to do something similar using TProcess, all I get back is an server error, saying it is a bad request.

Code: Pascal  [Select]
  1. AProcess := TProcess.Create(nil);
  2. url:='https://restapi.e-conomic.com/products';
  3. stoken:='demo';
  4. gtoken:='demo';
  5. AProcess.Executable:='curl';
  6. AProcess.Parameters.Add('-X GET ');
  7. AProcess.Parameters.Add('-H ');
  8. AProcess.Parameters.Add('"X-AppSecretToken:'+stoken+'" ');
  9. AProcess.Parameters.Add('-H ');
  10. AProcess.Parameters.Add('"X-AgreementGrantToken:'+gtoken+'" ');
  11. AProcess.Parameters.Add('-H ');
  12. AProcess.Parameters.Add('"Content-Type: application/json" ');
  13. AProcess.Parameters.Add(url);
  14.  
  15. AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
  16. AProcess.Execute;
  17. AStringList := TStringList.Create;
  18. AStringList.LoadFromStream(AProcess.Output);
  19.  
  20. writeln(AStringList.CommaText);    
  21.  

I have tried to play around using single quotes instead of double quotes, but I makes no change.

What have I overlooked?
« Last Edit: July 02, 2019, 11:24:56 am by chrnobel »

lucamar

  • Hero Member
  • *****
  • Posts: 2075
Re: Tprocess and Curl
« Reply #1 on: July 02, 2019, 02:53:46 am »
I don't know whether it's important or not, but shouldn't it be:
Code: Pascal  [Select]
  1. AProcess := TProcess.Create(nil);
  2. url:='https://restapi.e-conomic.com/products';
  3. stoken:='demo';
  4. gtoken:='demo';
  5. AProcess.Executable:='curl';
  6. AProcess.Parameters.Add('-X GET ');
  7. AProcess.Parameters.Add('-H "X-AppSecretToken:'+stoken+'" ');
  8. AProcess.Parameters.Add('-H "X-AgreementGrantToken:'+gtoken+'" ');
  9. AProcess.Parameters.Add('-H "Content-Type: application/json" ');
  10. AProcess.Parameters.Add(url);
  11.  
  12. AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
  13. AProcess.Execute;
  14. AStringList := TStringList.Create;
  15. AStringList.LoadFromStream(AProcess.Output);
  16.  
  17. writeln(AStringList.CommaText);

That is, make sure what is just one parameter gets passed as one parameter.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

chrnobel

  • Sr. Member
  • ****
  • Posts: 261
Re: Tprocess and Curl
« Reply #2 on: July 02, 2019, 08:39:49 am »
Hi lucamar.

I have actually also tried that, but the result is the same: Error 400 Bad Request, meaning something is actually send to the server, and the url seems to be right, but the header parameters are wrong.

TProcess.Commandline works:
Code: Pascal  [Select]
  1. AProcess.CommandLine:='curl -X GET -H "X-AppSecretToken: demo" -H "X-AgreementGrantToken: demo" '+
  2. '-H "Content-Type: application/json" https://restapi.e-conomic.com/products';

However commandline is deprecated, so I dont think that is the right path to follow.

Thaddy

  • Hero Member
  • *****
  • Posts: 9152
Re: Tprocess and Curl
« Reply #3 on: July 02, 2019, 08:46:40 am »
Why do you do not use libcurl directly?
also related to equus asinus.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7495
Re: Tprocess and Curl
« Reply #4 on: July 02, 2019, 10:06:03 am »
The initial, but then without any quoting. So remove " and trailing spaces

Code: Pascal  [Select]
  1. AProcess.Executable:='curl';
  2. AProcess.Parameters.Add('-X');
  3. AProcess.Parameters.Add('GET');
  4. AProcess.Parameters.Add('-H');
  5. AProcess.Parameters.Add('X-AppSecretToken:'+stoken);
  6. AProcess.Parameters.Add('-H);
  7. AProcess.Parameters.Add('X-AgreementGrantToken:'+gtoken);
  8. AProcess.Parameters.Add('-H');
  9. AProcess.Parameters.Add('Content-Type: application/json');
  10. AProcess.Parameters.Add(url);
  11.  

Url should also be free of quotes.

Keep in mind that quoting is something from shells. Here you execute over API directly, without spawning an additional shell, and you give all arguments separated -> quoting is redundant.
« Last Edit: July 02, 2019, 10:30:01 am by marcov »

chrnobel

  • Sr. Member
  • ****
  • Posts: 261
Re: Tprocess and Curl
« Reply #5 on: July 02, 2019, 10:08:46 am »
Why do you do not use libcurl directly?
I have tried to search, but the documentation is close to 100% absent, so where can I find a working example, and some documentation?

chrnobel

  • Sr. Member
  • ****
  • Posts: 261
Re: Tprocess and Curl
« Reply #6 on: July 02, 2019, 10:16:49 am »
The initial, but then without any quoting. So remove " and trailing spaces

Url should also be free of quotes.

I guess you mean:
Code: Pascal  [Select]
  1. url:='https://restapi.e-conomic.com/products';
  2. stoken:='demo';
  3. gtoken:='demo';
  4. AProcess.Executable:='curl';
  5. AProcess.Parameters.Add('-X GET');
  6. AProcess.Parameters.Add('-H');
  7. AProcess.Parameters.Add('X-AppSecretToken:'+stoken);
  8. AProcess.Parameters.Add('-H');
  9. AProcess.Parameters.Add('X-AgreementGrantToken:'+gtoken);
  10. AProcess.Parameters.Add('-H');
  11. AProcess.Parameters.Add('Content-Type: application/json');
  12. AProcess.Parameters.Add(url);
  13.  

Makes no difference.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7495
Re: Tprocess and Curl
« Reply #7 on: July 02, 2019, 10:30:30 am »
-X and GET also separate adds, sorry read over that. I updated my answer.

chrnobel

  • Sr. Member
  • ****
  • Posts: 261
Re: Tprocess and Curl
« Reply #8 on: July 02, 2019, 10:45:14 am »
-X and GET also separate adds, sorry read over that. I updated my answer.
Thanks marcov, that did the trick.