ColdFusion RESTful 网络服务

ColdFusion RESTful web service

我编写了一个 Web 服务,它从其他应用程序获取输入参数 (json)。我只是验证这些值并返回 "http status codes and response"(使用 cfheader)。为此,我正在 firefox 扩展中尝试 "RESTClient"。我的问题是 - 它是 运行 "GET" 方法成功但不是 POST method.I 猜测请求甚至不会进入 POST 方法中的服务器。请参阅下面两个请求的屏幕截图: 1.GET请求

  1. POST请求

这是我的 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>

仅供参考,GET 请求没有请求实体正文,因为它是 GET 请求,网络服务器通常会忽略请求中包含的任何正文。我对 ColdFusion 提供响应感到惊讶,因为我在您的 CFC 中没有看到 GET 方法。

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

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

使用CFFUNCTION中的'consumes'属性来区分。他们都可以在这之后调用一个公共函数。