[SOLVED] Response Code for Post request (fphttpclient) lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- yus · 2021-07-15 19:58 Hello! I try create POST request: 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 program project1; uses  Classes,  SysUtils,  fphttpclient,  opensslsockets; var  http: TFPHTTPClient;  answer: string;  str: string; begin  http := TFPHTTPClient.Create(nil);  str := '[624049117,2114097675]';       answer := http.SimpleFormPost('https:/ /esi.evetech.net/latest/characters/affiliation/?datasource=tranquility ', str);  writeln(answer);  WriteLn('Error Code: ', http.ResponseStatusCode);  answer := http.Get('https://esi.evetech.net /latest/characters/2114097675/?datasource=tranquility');  writeln(answer);  WriteLn('Error Code: ', http.ResponseStatusCode);  readln;  FreeAndNil(http);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 ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-07-15 21:31 Hey Yus, These are my investigations using httpie(command line http). First we need to create the file with the input: Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ echo "[624049117,2114097675]" > input.txt Then I tested with a GET to see what happens: Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ http --json --verbose "https://esi.evetech.net/latest/characters/affiliatio n/?datasource=tranquility"GET /latest/characters/affiliation/?datasource=tranquility HTTP/1.1Accept: application/json, */*;q=0.5Accept-Encoding: gzip, deflateConnection: keep-aliveContent-Type: application/jsonHost: esi.evetech.netUser- Agent: HTTPie/2.2.0   HTTP/1.1 405 Method Not AllowedAccess-Control- Allow-Credentials: trueAccess-Control-Allow-Headers: Content- Type,Authorization,If-None-Match,X-User-AgentAccess-Control-Allow- Methods: POST,OPTIONSAccess-Control-Allow-Origin: *Access-Control- Expose-Headers: Content-Type,Warning,ETag,X-Pages,X-ESI-Error-Limit- Remain,X-ESI-Error-Limit-ResetAccess-Control-Max-Age: 600Allow: POST,OPTIONSConnection: keep-aliveContent-Language: en-usContent- Length: 30Content-Type: application/json; charset=utf-8Date: Thu, 15 Jul 2021 21:01:40 GMTStrict-Transport-Security: max-age=31536000Vary: Accept-LanguageX-Esi-Error-Limit-Remain: 99X-Esi-Error-Limit-Reset: 20 {    "error": "method not allowed"} 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ http --json --verbose "https://esi.evetech.net/latest/characters/affiliatio n/?datasource=tranquility" < in.txt POST /latest/characters/affiliation/?datasource=tranquility HTTP/1.1Accept: application/json, */*;q=0.5Accept-Encoding: gzip, deflateConnection: keep-aliveContent-Length: 23Content-Type: application/jsonHost: esi.evetech.netUser-Agent: HTTPie/2.2.0 [    624049117,    2114097675] HTTP/1.1 200 OKAccess-Control-Allow-Credentials: trueAccess-Control-Allow-Headers: Content-Type,Authorization,If-None- Match,X-User-AgentAccess-Control-Allow-Methods: POST,OPTIONSAccess- Control-Allow-Origin: *Access-Control-Expose-Headers: Content- Type,Warning,ETag,X-Pages,X-ESI-Error-Limit-Remain,X-ESI-Error-Limit- ResetAccess-Control-Max-Age: 600Allow: OPTIONS,POSTConnection: keep- aliveContent-Length: 156Content-Type: application/json; charset=UTF-8Date: Thu, 15 Jul 2021 21:02:08 GMTStrict-Transport- Security: max-age=31536000X-Esi-Error-Limit-Remain: 100X-Esi-Error- Limit-Reset: 52X-Esi-Request-Id: 648f51fa-e265-4765-8c5e-fd04ff48154e [    {        "alliance_id": 1354830081,        "character_id": 2114097675,        "corporation_id": 98502318    },    {        "alliance_id": 99003581,        "character_id": 624049117,        "corporation_id": 98285865    }] The result is code 200 OK and then data is returned. QuoteSide 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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 ---------------------------------------------------------------------- yus · 2021-07-16 06:11 Change SimpleFormPost to FormPost. 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  answer := http.FormPost('https://esi.evetech.net/latest/characters/affiliation/? datasource=tranquility', str); ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-07-16 17:56 Hey Yus, Quote from: yus on July 16, 2021, 08:11:20 amChange SimpleFormPost to FormPost. And that was enough to get you a result? Ok, that works too Cheers, Gus ---------------------------------------------------------------------- yus · 2021-07-19 18:43 Quote from: Gustavo 'Gus' Carreno on July 16, 2021, 07:56:18 pmAnd that was enough to get you a result? Yes. ---------------------------------------------------------------------- krolikbest · 2021-07-19 19:25 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,