Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
post xml data with XMLHttpRequest
#1
Hi ATOZED,

Similar to the example "PostDataDemo", I want to query XML data via a corresponding content handler, but my content handler cannot find the query data.

C++ Builder 12 Patch 1
Edition: IntraWeb Ultimate Edition
IntraWeb Version: 15.6.3
32-bit Server on 64-bit Windows
HTTP Server is active on port: 80
HTTPS Server is active on port: 543 (OpenSSL v1.1.1)

In the server controller, the content handler is configured as follows:

this->SecurityOptions->HttpMethods << hmGet << hmPost;
this->SecurityOptions->ForceAjaxPost = true;

TIWMimeTypes::RegisterType(L".xml", L"application/xml; charset=UTF-8", false);
THandlers::Add(L"", L"Query.xml", new TIWH_Xml());

The query is made interactively via an XMLHttpRequest object (see appendix).
The HTML document is inserted into a frame of the MainForm.

The content handler is called correctly but the query data does not arrive:

aRequest->HasContent = true
aRequest->Files->Count = 0 !!!

The test tool specified in "How to simulate a request using Firefox Poster addon.png" (2017)
I could unfortunately no longer find it at Mozilla.

Is there something wrong with my example ?
Does anything else need to be configured ?

Regards
Juergen


Attached Files
.html   Query.html (Size: 691 bytes / Downloads: 1)
Reply
#2
Hey Juergen

I got nearly the same problem, and I could solve it by reading this thread:
https://www.atozed.com/forums/thread-1402.html

I add these in ServerController.cpp and it works:
void __fastcall TIWServerController::IWServerControllerBaseConfig(TObject *Sender)
         
{
  TpaypalForm::SetURL("/", "paypalForm"); 
  //RegisterContentType(L"application/xml");   
  RegisterContentType(L"application/json");    //  <---------------------
}

Maybe it helps

Regards
 Jörg
Reply
#3
Hi Jörg

thanks for the hint.

I added the following entry in the Server Controller and it works,
the XML content is transferred as a file to the server cache.

// Content-Type from IW.Parser.Files.hpp
RegisterContentType(L"application/xml");

However, I now have a new problem Wink
No matter how I try to read the file content in the content handler I always get an exception.

Message: "File ...\cache\01e73j1t0q\ATZFH~UucD5kIPr~szeJN7wnmOoy7W.tmp cannot be opened. The process cannot access the file because it is being used by another process."

Regards
Juergen
Reply
#4
I used this code , it's the execute func of the content handler:

bool __fastcall TContentPP ::Execute(Iw::Http::Request::THttpRequest* aRequest, Iw::Http::Reply::THttpReply* aReply, const System::UnicodeString aPathname, Iwapplication::TIWApplication* aSession, System::Classes::TStrings* aParams)
{
bool          rc = true;
UnicodeString  empf;
THttpFile    *xFile;
TFileStream  *fs;
TStringStream *js;

if (aRequest->Files->Count > 0)
  {
    xFile = aRequest->Files->Items[0];
    fs = new TFileStream(xFile->TempPathName, fmOpenRead | fmShareDenyNone);
    js = new TStringStream;
   

    js->CopyFrom(fs, 0);
    empf = js->DataString;
   
    delete fs;
    delete js;
    rc = true;
  }
 
if (aReply)
  {
  aReply->ContentType = MIME_JSON;
  aReply->Code = 200;
  aReply->CodeText = "OK";
  aReply->WriteString("{\"status\":\"success\"}"); // R?ckgabe eines JSON-Objekts 
  }
 
 
return rc; 
}
//---------------------------------------------------------------------------
the string "empf" hold the json-reply I wait for.
Reply
#5
I'am using the same code, there must be another reason.
Reply
#6
hmmmm
I don't know the reason ... but maybe you can try a shorter filename . I.e. the component TIWURLWindow comes with a similar error , when trying such a long URI name. Don't know why, but a shorter filename works.
Reply
#7
I'm not able to test this but what happens if you execute the code below to read the JSON?


Code:
...

if(aRequest->Files->Count > 0) {
  String empf = aRequest->Files->Items[0]->ReadAllText();
}

...
Reply
#8
I used the following code with the same result:

UnicodeString CurXmlStr = TIWTextFileReader::ReadAllText(aRequest->Files->Items[0]->TempPathName);
Reply
#9
Did this 'aRequest->Files->Items[0]->ReadAllText();' work though?  The idea being to use what's built in the THttpFile object and not open it a second time.
Reply
#10
(08-26-2024, 06:10 PM)MJS@mjs.us Wrote: Did this 'aRequest->Files->Items[0]->ReadAllText();' work though?  The idea being to use what's built in the THttpFile object and not open it a second time.

Worked, thanks a lot !
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)