如何在coldfusion cfc中接收JSON POST请求

时间:2017-07-21 09:13:12

标签: coldfusion cfc

我有一个Web服务(cfc),它将从外部应用程序捕获/接收将发布信息的JSON数据。 输入请求将采用类似于以下格式的JSON: enter image description here

所以我将收到CustomerId,UserName和Password,我只需要在我的数据库中验证这些字段并返回成功/失败消息。

我的问题是 - 如何在我的cfc中接收json数据?我写了表格范围,因为我相信它会解决我的目的,但我不确定。 另外如何解析json值?

以下是我的CFC:

<cfcomponent rest="true" restpath="/AimsWeb"> <!--- REST Service--->

<cffunction name="AuthenticateUser" access="remote" httpmethod="GET"  returntype="any">

<!---- Defining Arguments--->
    <cfargument name="Username" type="string" required="Yes">
    <cfargument name="Password" type="string" required="Yes">
    <cfargument name="CustomerID" type="string" required="Yes">

<!---- Setting the Form Values (which we will get from AW+) and setting it to arguments passed--->
    <cfset Form.CustomerID = arguments.CustomerID>
    <cfset Form.Username = arguments.Username>
    <cfset Form.Password = arguments.Password>

<cfif StructKeyExists (form, 'CustomerID') and StructKeyExists(form, 'UserName') and StructKeyExists (form, 'password')>
   <cfquery name="AllUsers" datasource="#Application.GomDatasource#">
    SELECT u.UserTypeID, u.UserID, u.CustomerID, u.UserName, u.Password, u.active, u.locked
     FROM tblUsers u
     WHERE u.username = 'vasu'
    AND  u.CustomerID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Form.CustomerId#">
    <!---  OR u.username = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Form.userName#"> --->
<!---    OR u.password = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Form.password#"> --->
   </cfquery>

<!--- This is to check whether provided parameters are valid by checking the same in the database--->
<cfset local.StatusStruct = StructNew()>
<!--- <cfdump var="#AllUsers#"> --->
    <cfif AllUsers.RecordCount AND (AllUsers.Active EQ 0 OR AllUsers.locked EQ 1)>
        <cfset local.StatusStruct['errorCode'] = 401>
        <cfset local.StatusStruct['errorMessage'] = " User Account is locked">
    <cfelse>

        <cfif form.customerid EQ "" OR form.username EQ "" OR form.password EQ "">
            <cfset local.StatusStruct['errorCode'] = 400>
            <cfset local.StatusStruct['errorMessage'] = " Insufficient Input. Please provide Customer ID, UserName and Password">

        <cfelseif AllUsers.RecordCount AND form.CustomerId EQ AllUsers.CustomerID AND form.username EQ AllUsers.UserName AND form.password EQ AllUsers.Password>
            <cfset local.StatusStruct['errorCode'] = 200>
            <cfset local.StatusStruct['errorMessage'] = " Success">

        <cfelseif AllUsers.CustomerID NEQ form.CustomerID>
            <cfset local.StatusStruct['errorCode'] = 400>
            <cfset local.StatusStruct['errorMessage'] = " Customer Id doesn't exist">

         <cfelseif AllUsers.UserName NEQ form.UserName>
            <cfset local.StatusStruct['errorCode'] = 400>
            <cfset local.StatusStruct['errorMessage'] = " User not found">

         <cfelseif AllUsers.Password NEQ form.password>
            <cfset local.StatusStruct['errorCode'] = 400>
            <cfset local.StatusStruct['errorMessage'] = " Invalid Password">

        </cfif>

    </cfif>
</cfif>
<cfreturn local.StatusStruct> <!--- Returning the status in JSON form--->
  </cffunction>



</cfcomponent>

1 个答案:

答案 0 :(得分:-1)

结帐发布/获取请求是在coldFusion中完成的

  <cfoutput> cgi.request_method=#cgi.request_method#<br/> </cfoutput>
 <cfset req = getHTTPRequestData()> 
<cfoutput> getHTTPRequestData().method=#req.method# </cfoutput>