Recent

Author Topic: [SOLVED] Response Code for Post request (fphttpclient)  (Read 4069 times)

yus

  • Jr. Member
  • **
  • Posts: 57
[SOLVED] Response Code for Post request (fphttpclient)
« on: July 15, 2021, 09:58:51 pm »
Hello! I try create POST request:
Code:

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. uses
  4.   Classes,
  5.   SysUtils,
  6.   fphttpclient,
  7.   opensslsockets;
  8.  
  9. var
  10.   http: TFPHTTPClient;
  11.   answer: string;
  12.   str: string;
  13.  
  14. begin
  15.   http := TFPHTTPClient.Create(nil);
  16.   str := '[624049117,2114097675]';    
  17.   answer := http.SimpleFormPost('https://esi.evetech.net/latest/characters/affiliation/?datasource=tranquility', str);
  18.   writeln(answer);
  19.   WriteLn('Error Code: ', http.ResponseStatusCode);
  20.   answer := http.Get('https://esi.evetech.net/latest/characters/2114097675/?datasource=tranquility');
  21.   writeln(answer);
  22.   WriteLn('Error Code: ', http.ResponseStatusCode);
  23.   readln;
  24.   FreeAndNil(http);
  25. end.

Why for POST request error code always = 0?
GET request is good.

Thanks.

PS:
Lazarus 2.1.0
FPC Version: 3.3.1
SVN Revision: 65168
« Last Edit: July 16, 2021, 08:11:32 am by yus »

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1090
  • Professional amateur ;-P
Re: Response Code for Post request (fphttpclient)
« Reply #1 on: July 15, 2021, 11:31:39 pm »
Hey Yus,

These are my investigations using httpie(command line http).

First we need to create the file with the input:
Code: Bash  [Select][+][-]
  1. $ echo "[624049117,2114097675]" > input.txt

Then I tested with a GET to see what happens:
Code: Bash  [Select][+][-]
  1. $ http --json --verbose "https://esi.evetech.net/latest/characters/affiliation/?datasource=tranquility"
  2. GET /latest/characters/affiliation/?datasource=tranquility HTTP/1.1
  3. Accept: application/json, */*;q=0.5
  4. Accept-Encoding: gzip, deflate
  5. Connection: keep-alive
  6. Content-Type: application/json
  7. Host: esi.evetech.net
  8. User-Agent: HTTPie/2.2.0
  9.  
  10.  
  11.  
  12. HTTP/1.1 405 Method Not Allowed
  13. Access-Control-Allow-Credentials: true
  14. Access-Control-Allow-Headers: Content-Type,Authorization,If-None-Match,X-User-Agent
  15. Access-Control-Allow-Methods: POST,OPTIONS
  16. Access-Control-Allow-Origin: *
  17. Access-Control-Expose-Headers: Content-Type,Warning,ETag,X-Pages,X-ESI-Error-Limit-Remain,X-ESI-Error-Limit-Reset
  18. Access-Control-Max-Age: 600
  19. Allow: POST,OPTIONS
  20. Connection: keep-alive
  21. Content-Language: en-us
  22. Content-Length: 30
  23. Content-Type: application/json; charset=utf-8
  24. Date: Thu, 15 Jul 2021 21:01:40 GMT
  25. Strict-Transport-Security: max-age=31536000
  26. Vary: Accept-Language
  27. X-Esi-Error-Limit-Remain: 99
  28. X-Esi-Error-Limit-Reset: 20
  29.  
  30. {
  31.     "error": "method not allowed"
  32. }

So as you can see the GET should give you a 405 Method Not Allowed and not an OK like you reported.

Once you do a POST with the data you provided:
Code: Bash  [Select][+][-]
  1. $ http --json --verbose "https://esi.evetech.net/latest/characters/affiliation/?datasource=tranquility" < in.txt
  2. POST /latest/characters/affiliation/?datasource=tranquility HTTP/1.1
  3. Accept: application/json, */*;q=0.5
  4. Accept-Encoding: gzip, deflate
  5. Connection: keep-alive
  6. Content-Length: 23
  7. Content-Type: application/json
  8. Host: esi.evetech.net
  9. User-Agent: HTTPie/2.2.0
  10.  
  11. [
  12.     624049117,
  13.     2114097675
  14. ]
  15.  
  16. HTTP/1.1 200 OK
  17. Access-Control-Allow-Credentials: true
  18. Access-Control-Allow-Headers: Content-Type,Authorization,If-None-Match,X-User-Agent
  19. Access-Control-Allow-Methods: POST,OPTIONS
  20. Access-Control-Allow-Origin: *
  21. Access-Control-Expose-Headers: Content-Type,Warning,ETag,X-Pages,X-ESI-Error-Limit-Remain,X-ESI-Error-Limit-Reset
  22. Access-Control-Max-Age: 600
  23. Allow: OPTIONS,POST
  24. Connection: keep-alive
  25. Content-Length: 156
  26. Content-Type: application/json; charset=UTF-8
  27. Date: Thu, 15 Jul 2021 21:02:08 GMT
  28. Strict-Transport-Security: max-age=31536000
  29. X-Esi-Error-Limit-Remain: 100
  30. X-Esi-Error-Limit-Reset: 52
  31. X-Esi-Request-Id: 648f51fa-e265-4765-8c5e-fd04ff48154e
  32.  
  33. [
  34.     {
  35.         "alliance_id": 1354830081,
  36.         "character_id": 2114097675,
  37.         "corporation_id": 98502318
  38.     },
  39.     {
  40.         "alliance_id": 99003581,
  41.         "character_id": 624049117,
  42.         "corporation_id": 98285865
  43.     }
  44. ]

The result is code 200 OK and then data is returned.

Quote
Side Note:
It's imperative that you have good debugging tools at your disposal when you're dealing with HTTP services.
You need to FIRST be sure of what the service needs in order to THEN do a good job in code.
httpie is a good command line tool if you like the CLI life.
There's also Postman if you're more into the GUI life.
Delphi also comes with a GUI client for this purpose, but after ~3 simple Google searches I couldn't find a link to it, sorry

As a principal rule of thumb when you're making a call to a service that deals in JSON you should AT LEAST include the following header:
Code: Pascal  [Select][+][-]
  1. http.AddHeader('Accept: application/json');

And if you're POSTing any JSON, then you should, also as a rule of thumb, include the following header:
Code: Pascal  [Select][+][-]
  1. http.AddHeader('Content-Type: application/json')

Only then can you be sure that you've made sure to adhere to the most basics of the specs in terms of any JSON webservice.

Add those headers to your code and then tell us if anything changed.

Cheers,
Gus


« Last Edit: July 15, 2021, 11:35:14 pm by Gustavo 'Gus' Carreno »
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

yus

  • Jr. Member
  • **
  • Posts: 57
Re: Response Code for Post request (fphttpclient)
« Reply #2 on: July 16, 2021, 08:11:20 am »
Change SimpleFormPost to FormPost.

Code: Pascal  [Select][+][-]
  1.  answer := http.FormPost('https://esi.evetech.net/latest/characters/affiliation/?datasource=tranquility', str);

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1090
  • Professional amateur ;-P
Re: Response Code for Post request (fphttpclient)
« Reply #3 on: July 16, 2021, 07:56:18 pm »
Hey Yus,

Change SimpleFormPost to FormPost.

And that was enough to get you a result?

Ok, that works too :)

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

yus

  • Jr. Member
  • **
  • Posts: 57
Re: Response Code for Post request (fphttpclient)
« Reply #4 on: July 19, 2021, 08:43:42 pm »
And that was enough to get you a result?

Yes. :)

krolikbest

  • Full Member
  • ***
  • Posts: 246
Re: [SOLVED] Response Code for Post request (fphttpclient)
« Reply #5 on: July 19, 2021, 09:25:26 pm »
Hi
 there it is an explanation (by Leledumbo) why in that way and not the other
 https://forum.lazarus.freepascal.org/index.php/topic,37563.msg252820.html#msg252820

Regards,

 

TinyPortal © 2005-2018