Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ContentHandler and PostData
#1
Hi, I am struggling to get raw json data via a post to my contenthandler.

Code:
$.ajax({type: "POST",
   url: "/url-to-my-content-handler",
    data: {"field1": "fieldvalue", "field2", "field2value"},
    contentType: "application/json; charset=utf-8",
    datatype: "json"
   })


So how can I fetch the post data in the contenthandler? ContentFields will separate all fields into something like this (mostly)
field1=fieldvalue
field2=field2value

because my json structure is a little bit complexer (contains further objects and arrays) than in the demo this is not the way I can go.

I am missing somethling like aRequest.PostData as String or TStream. Is it there but I can not find it?

The XML-Example is using uploading a file but that's not the way I go here.

Best regards
Sven
Reply
#2
Are you referring to the PostDataDemo?

If that's the case, IW will always receive the content as a "file" (from the class THttpFile, declared in IW.HTTP.FileItem.pas), doesn't matter how big is the content that is being received and how it is actually sent from the browser.

So, this code should work for you the same way, considering a TContentJson class:


Code:
function TContentJson.Execute(aRequest: THttpRequest; aReply: THttpReply; const aPathname: string; aSession: TIWApplication;
  aParams: TStrings): boolean;
var
  json: string;
begin
  if aRequest.Files.Count = 1 then begin
    json:= THttpFile(aRequest.Files[0]).ReadAllText;   // <- here you have the whole Json sent to your application as a string
  end;
end;

If you want to use IW built in classes to deal with the Json object as well (which I recommend), you can do something like this:

Code:
uses
  IWJsonDataObjects;

function TContentJson.Execute(aRequest: THttpRequest; aReply: THttpReply; const aPathname: string; aSession: TIWApplication;
  aParams: TStrings): boolean;
var
  jsonString: string;
  jsonObject: TJsonObject;
begin
  if aRequest.Files.Count = 1 then begin
    jsonString:= THttpFile(aRequest.Files[0]).ReadAllText;
    jsonObject := TJsonObject.Parse(jsonString) as TJsonObject;  // <- here you have a fully functional json object and you can use TJsonObjects methods and properties
  end;
end;

The library used here (IWJsonDataObjects) is actually JsonDataObjects created by Andreas Hausladen (https://github.com/ahausladen/JsonDataObjects), possibly the best Delphi library to deal with Json objects (and also the fastest).

Remember that you need to register the expected content-type otherwise IW will discard it (because IW won't receive and process unknown content types, unless you explicitly instruct it to do so). In your server controller, do this:

Code:
uses
  IW.Parser.Files;

procedure TIWServerController.IWServerControllerBaseConfig(Sender: TObject);
begin
  RegisterContentType('application/json');
end;
Reply
#3
Thank you, Alexandre,

I tried it with the "files approach".

But -aRequest.Files.Count- is always zero. May be I missed the registration of the contenttype. I will try it with this.


Sven
Greetings
Sven
Reply
#4
Yes, Sven, without the registration of the type, the number of files will always be zero. IW will just ignore unknown content as a security risk.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)