在 Coldfusion 中读取传入的 xml

read incoming xml in Coldfusion

我需要编写的是一个类似于 SOCKET 的脚本。客户端 post 一个 xml 到我的端点,我的脚本检查它是否有效 xml。如果是表单上传(知道输入文件名),我可以这样做。但是如果我们不知道输入名称怎么办?如何捕获文件并检查它是否有效xml?

<!--- that's the POST request that was sent to us (supposed to contain XML in body) --->
<cfset httpRequestData = getHttpRequestData()>

<!--- NOTE: you don't need to check the Content-Type, but I consider it to be best practise --->
<cfif (
    structKeyExists(httpRequestData.Headers, "Content-Type") and
    (httpRequestData.Headers["Content-Type"] contains "/xml") <!--- covers "application/xml" and "text/xml" --->
)>

    <!--- in case we receive raw bytes, encode them --->
    <cfif isBinary(httpRequestData.Content)>
        <cfset httpBody = toString(httpRequestData.Content, "UTF-8")>
    <cfelse>
        <cfset httpBody = httpRequestData.Content>
    </cfif>

    <cfif isXml(httpBody)>

        <cfset xmlDoc = xmlParse(httpBody)>

        <cfdump var="#xmlDoc#">

    <cfelse>
        <cfoutput>Error: Body seems to contain malformed XML.</cfoutput>
    </cfif>

<cfelse>
    <cfoutput>Error: No Content-Type provided.</cfoutput>
</cfif>