Subscribe for automatic updates: RSS icon RSS

Login icon Sign in for full access | Help icon Help
Advanced search

Pages: [1]
  Reply  |  Print  
Author Topic: Error loading XML string from com.HttpServiceRequest.readFormEncodedRequest  (Read 2252 times)
Randy H.
Posts: 1


« on: January 03, 2024, 08:10:33 pm »

When using readFormEncodedRequest() to get the query of a POST "application/x-www-form-urlencoded" request, the resulting string cannot be loaded into a DOM document when there are escaped special characters.

The special characters & and = are escaped by doubling them per http://4js.com/online_documentation/fjs-fgl-manual-html/#fgl-topics/c_gws_ComHTTPServiceRequest_readFormEncodedRequest.html

-- incoming data (simplified)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.024/cXML.dtd">
<cXML>
    <Message>
        <Manufacturer>Retail Acquisition &amp; Development, Inc.</Manufacturer>         
    </Message>
</cXML>

-- parsed
define in_xmlStr string
let in_xmlStr = in_context.httpServiceRequest.readFormEncodedRequest(FALSE)

This results in the double && on the Manufacturer line

<!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.024/cXML.dtd">
<cXML>
    <Message>
        <Manufacturer>Retail Acquisition &&amp; Development, Inc.</Manufacturer>
    </Message>
</cXML>

--Loading the string into a DOM document fails
define docXML xml.DomDocument
let docXML = xml.DomDocument.create()

  try
     call docXML.loadFromString(in_xmlStr)
  catch
         -- error handling code caught
  end try


The caught error message is "Error calling docXML.loadfromstring"

Removing the double && resolves the issue. Is there a way to do it natively without this code?

let lv_buffer = base.StringBuffer.create()
 call lv_buffer.append(in_xmlStr)
call lv_buffer.replace("&&","&",0)

Reuben B.
Four Js
Posts: 1062


« Reply #1 on: January 05, 2024, 02:37:57 am »

I am wondering why you are using readFormEncodedRequest.  The content is expected to be key-value tuples, separated by & and with = separating key and value.   The doubling of the special characters simplifies the separation into key-value tables and then again simplifies the splitting of key from value.  Once you have the value separated then as you noted you have to use replace to turn && back to & and == back to =

If the input is not key-value tuples but an XML document then I wonder if readXmlRequest() is the better option instead of readFormEncodedRequest.

Reuben

Product Consultant (Asia Pacific)
Developer Relations Manager (Worldwide)
Author of https://4js.com/ask-reuben
Contributor to https://github.com/FourjsGenero
terro r.
Posts: 1


« Reply #2 on: February 21, 2024, 11:04:57 am »

The message "Error calling docXML.loadfromstring" has been recognized as an error. I question whether readXmlRequest() is a preferable choice to readFormEncodedRequest in the case where the input is an XML document rather than a key-value dataset. eggy car
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines