ColdFusion RESTful Web服务

时间:2017-07-23 19:27:48

标签: web-services coldfusion restful-authentication cfc

我编写了一个Web服务,它从其他应用程序获取输入参数(json)。我只是验证这些值并返回" http状态代码和响应" (使用cfheader)。为此,我正在尝试" RESTClient"在Firefox扩展。我的问题是 - 它正在运行" GET"方法成功但不是POST方法。我猜这个请求甚至没有在POST方法中进入服务器。请参阅下面两个请求的屏幕截图: 1. GET请求 enter image description here

  1. POST请求 enter image description here
  2. 这是我的CFC:

    <cfcomponent rest="true" restpath="/AimsWeb"> <!--- REST Service--->
    
    <cffunction name="AuthenticateUser" access="remote" httpmethod="POST"  returntype="void">
    
    <!---- Defining Arguments--->
        <cfargument name="Username" type="string" required="Yes">
        <cfargument name="Password" type="string" required="Yes">
        <cfargument name="CustomerID" type="numeric" 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>
    
    <!--- Take input json, parse it and set in in a variable --->
    <cfscript>
      record=deserializeJSON(
    '{
    "customerId": #Form.CustomerID#,
    "userName": "#Form.userName#",
    "password": "#Form.Password#"
    }'
    );
    
    this.customerid = record.customerId;
    this.userName = record.userName;
    this.password = record.password;
     </cfscript>
    
       <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.CustomerID =  <cfqueryparam cfsqltype="cf_sql_integer" value="#this.customerid#">
            AND  u.username =  <cfqueryparam cfsqltype="cf_sql_varchar" value="#this.username#">
            AND u.password = <cfqueryparam cfsqltype="cf_sql_varchar" value="#this.password#">
       </cfquery>
    
    <!--- This is to check whether provided parameters are valid by checking the same in the database--->
    <cfset local.StatusStruct = StructNew()>
    
        <cfif AllUsers.RecordCount AND (AllUsers.Active EQ 0 OR AllUsers.locked EQ 1)>
            <cfheader statuscode="401" statustext="User Account is locked">
        <cfelse>
    
            <cfif this.customerid EQ "" OR this.username EQ "" OR this.password EQ "">
                <cfheader statuscode="400" statustext="Insufficient Input. Please provide Customer ID, UserName and Password">
            <cfelseif AllUsers.RecordCount AND this.CustomerId EQ AllUsers.CustomerID AND this.username EQ AllUsers.UserName AND this.password EQ AllUsers.Password>
                <cfheader statuscode="200" statustext="Success">
            <cfelseif AllUsers.CustomerID NEQ this.CustomerID>
                <cfheader statuscode="400" statustext="Customer Id doesn't exist">
            <cfelseif AllUsers.UserName NEQ this.UserName>
                <cfheader statuscode="400" statustext="User not found">
            <cfelseif AllUsers.Password NEQ this.password>
                <cfheader statuscode="400" statustext="Invalid Password">
            </cfif>
        </cfif>
      </cffunction>
    </cfcomponent>
    

1 个答案:

答案 0 :(得分:0)

FYI GET请求没有请求实体主体,因为它是GET请求,Web服务器通常会忽略请求中包含的任何主体。我很惊讶ColdFusion提供了一个回复,因为我在你的CFC中没有看到GET方法。

POST方法可能失败,因为CFC期望MIME类型为application / form-url-encoded(您的CFARGUMENTs)并且您正在发布application / json

尝试分解为2个函数,一个接受application / json,一个application / form-url-encoded

使用&#39;消费&#39;在CFFUNCTION中的属性来区分。在此之后,他们都可以调用一个共同的函数。