fphttpapp request content empty
lazarus / 38-Networking and Web Programming
----------------------------------------------------------------------
michoux · 2023-01-21 09:30
Hello,
I have some problem with fphttpapp
I have follogin html code which calls my local service coded in
fphttpapp
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
Localhost printing test
Call Here
is just a small code to get the content of the above request.
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 procedure
acr(req: TRequest; res: TResponse);var rawJson: AnsiString; begin
rawJson:=req.Content; writeln(rawjson); end;
If for example the json data in html is not correctly encoded in
brackets Like this data: '[{"status":"Odobren"}]', then I do not
receive anything on my local service in req.Content;
Is there any way to check if there was actuall connection made on
fphttpapp?
----------------------------------------------------------------------
Leledumbo · 2023-01-22 05:49
I can only guess, but are you sure you set up the https termination
correctly? I would try http first. And after you solve that, ensure
you handle CORS correctly or serve the client HTML script from the
server, as the base address must match in order not to trigger CORS.
fphttpserver / fphttpapp does not do any automatic body conversion and
the request content should be readable as is, provided it's actually
gone through. As an example, using curl I can even hit broken JSON to
your code:
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 $ curl
localhost:8000/ -H 'content-type: application/json' -d '[{"x":"y"]'and
it gets printed just fine.
----------------------------------------------------------------------
michoux · 2023-01-22 10:02
Hi,
Now I fixed it, I mean it was fixed from web site.
Another issue is that data which I receive is like this.
data%5B0%5D%5Bvip%5D=0&data%5B0%5D%5Bfirstname%5D=Mojca&data%5B0%5D%5B
lastname%5D=Smolej&data%5B0%5D%5Bcountry%5D=Slovenija&data%5B0%5D%5Bac
creditationNumber%5D=2023001000372&data%5B0%5
I am expecting JSON data but what I get is not JSON.
I try to speak with developers from this web site and they claim that
they are sending proper JSON data.
Can I do something with above data, or I need to discuss with web
developers to send me correct.
----------------------------------------------------------------------
TRon · 2023-01-22 10:09
Quote from: michoux on January 22, 2023, 11:02:49 amI am expecting
JSON data but what I get is not JSON.
It /is/ JSON
You need to decode the HTML entities to plain text. see also:
https://stackoverflow.com/questions/9966053/what-does-5b-and-5d-in-
post-requests-stand-for
edit:
I agree that this is a very odd way of returning JSON values (e.g. not
standard)
simple example:
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
test; {$mode objfpc}{$H+} uses sysutils, strutils; var encoded :
string = 'data%5B0%5D%5Bvip%5D=0&data%5B0%5D%5Bfirstname%5D=Mojca&data
%5B0%5D%5Blastname%5D=Smolej&data%5B0%5D%5Bcountry%5D=Slovenija&data%5
B0%5D%5BaccreditationNumber%5D=2023001000372&data%5B0%5'; decoded :
string; splitted : TStringArray; part : string;begin
writeln('encoded = ', encoded); decoded := encoded; decoded :=
StringReplace(decoded, '%5B', '[', [rfReplaceAll, rfIgnorecase]);
decoded := StringReplace(decoded, '%5D', ']', [rfReplaceAll,
rfIgnorecase]); writeln('decoded = ', decoded); writeln;
writeln('splitted:'); splitted := SplitString(decoded, '&'); for
part in splitted do writeln(part);end.
which returns:
Code: [Select]encoded = data%5B0%5D%5Bvip%5D=0&data%5B0%5D%5Bfirstname
%5D=Mojca&data%5B0%5D%5Blastname%5D=Smolej&data%5B0%5D%5Bcountry%5D=Sl
ovenija&data%5B0%5D%5BaccreditationNumber%5D=2023001000372&data%5B0%5
decoded = data[0][vip]=0&data[0][firstname]=Mojca&data[0][lastname]=Sm
olej&data[0][country]=Slovenija&data[0][accreditationNumber]=202300100
0372&data[0%5
splitted:
data[0][vip]=0
data[0][firstname]=Mojca
data[0][lastname]=Smolej
data[0][country]=Slovenija
data[0][accreditationNumber]=2023001000372
data[0%5
----------------------------------------------------------------------
dseligo · 2023-01-22 18:30
OT suggestion: remove personal data from examples (there is name and
some accreditationNumber).
----------------------------------------------------------------------
Leledumbo · 2023-01-23 03:23
Quote from: michoux on January 22, 2023, 11:02:49 amAnother issue is
that data which I receive is like this.
data%5B0%5D%5Bvip%5D=0&data%5B0%5D%5Bfirstname%5D=Mojca&data%5B0%5D%5B
lastname%5D=Smolej&data%5B0%5D%5Bcountry%5D=Slovenija&data%5B0%5D%5Bac
creditationNumber%5D=2023001000372&data%5B0%5
I am expecting JSON data but what I get is not JSON.
I try to speak with developers from this web site and they claim that
they are sending proper JSON data.
Can I do something with above data, or I need to discuss with web
developers to send me correct.
I can't tell, is this what req.Content spits? Or is it a result of
your code decoding it? Certainly it is not JSON, it's a urlencoded
instead:
Quote"the keys and values are encoded in key-value tuples separated by
'&', with a '=' between the key and the value"which is exactly what
your string is, if you urldecode that %XX chars:
Code: [Select]data[0][vip]=0&data[0][firstname]=Mojca&data[0][lastname
]=Smolej&data[0][country]=Slovenija&data[0][accreditationNumber]=20230
01000372&data[0%5(yes, you gave truncated data)