0
votes

I have a web service (cfc) which will catch/receive JSON data from an external application that would be posting information. The input request will be in JSON similar to below format: enter image description here

So I will be receiving CustomerId, UserName and Password and I just need to validate this fields in my database and return the success/failure message.

My question is - How to receive the json data in my cfc? I have written Form scope as I believe it would solve my purpose, but I am not sure. Also how to parse that json values?

Below is my 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>
You can receive it as a string. Then do a google search on coldfusion deserializejson. - Dan Bracuk
@DanBracuk: ya, I am doing that. I have tried: <cfset password=DeserializeJSON(Form.Password)>, and similarly setting other values. But not working. - Vasu
Start with something simpler. How about <cfdump var="#DeserializeJSON(some json string)#">. Look at what that gives you and take it from there. - Dan Bracuk
ok, let me try that out. - Vasu
I have used - serializejson to create a json string and then parsed it in : <cfset jsonData = DeserializeJSON(variables.dataFields)> <cfdump var="#jsonData#" label="swap">. I have got a 'Struct' with three values : CustomerID: 100, Password: xxx, userName: test. - Vasu