Forum > Networking and Web Programming

TFPHTTPClient debugging

(1/6) > >>

SymbolicFrank:
It works, it's pretty simple to get going, but it is hard to see what is happening and it likes raising exceptions (there are 15). Like this:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TFPCustomHTTPClient.Get(const AURL: String; Stream: TStream);begin  HTTPMethod('GET',AURL,Stream,[200]);end;
The HTTPMethod is the main function to execute the request. Which ends up in:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Function TFPCustomHTTPClient.ReadResponse(Stream: TStream;  const AllowedResponseCodes: array of Integer; HeadersOnly: Boolean): Boolean;  //...  if not CheckResponseCode(FResponseStatusCode,AllowedResponseCodes) then    Raise EHTTPClient.CreateFmt(SErrUnexpectedResponse,[ResponseStatusCode]);
Where that "[200]" is the AllowedResponseCodes. So, if the result is not 200, you don't get to see the response, because it isn't read.

Also, seeing what message (and headers!) is actually sent is quite hard.

Is the intended use of this class that you override virtual methods until you can see what is happening?

SymbolicFrank:
How do you add parameters?

Thaddy:
200 is not the only legal response code. 301 and 302 are too (redirections, there are more...like for old security protocols) and quite very common nowadays.
https://en.wikipedia.org/wiki/HTTP_301
https://en.wikipedia.org/wiki/HTTP_302

So if you expect 200 rightaway you are making a programmer error.
Simply set AllowRedirects to true. Eventually you should get 200.

Gustavo 'Gus' Carreno:
Hey SymbolicFrank,


--- Quote from: SymbolicFrank on May 20, 2022, 04:23:03 pm ---Also, seeing what message (and headers!) is actually sent is quite hard.

--- End quote ---

If you look at the code (https://gitlab.com/freepascal.org/fpc/source/-/blob/main/packages/fcl-web/src/base/fphttpclient.pp#L373) you can see that TFPHTTPClient exposes these, among many:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  TFPHTTPClient = Class(TFPCustomHTTPClient)  Published    { Shortened to make a point }    Property RequestHeaders;    Property RequestBody;    Property ResponseHeaders;    Property ResponseStatusCode;    Property ResponseStatusText;    Property Cookies;    Property Proxy;  end;
If you're looking to inspect the Request Headers before you perform a method, that would be RequestHeaders.
If you're looking to inspect the Request Body, usually empty if you don't place anything there, that would be RequestBody.
If you want to inspect the Response Headers after you've performed a method, that would be ResponseHeaders.

Cheers,
Gus

Gustavo 'Gus' Carreno:
Hey SymbolicFrank,


--- Quote from: SymbolicFrank on May 23, 2022, 03:09:33 pm ---How do you add parameters?
--- End quote ---

If you're looking for a way to add parameters like one is used to on Postman, or some other HTTP Client object implementations you will not find any.

It's up to you to add the question mark symbol and the name, value pairs in the URL string that you pass to any method call.
Same with the anchor side of things: it's up to you to put the hash symbol and the name of the anchor if you want to have that in.

But I do agree that it would be a good Quality of Life type of feature that could be added without much effort.

Cheers,
Gus

Navigation

[0] Message Index

[#] Next page

Go to full version