简体   繁体   中英

ColdFusion RESTful web service

i have written a web service which takes input parameters (json) from other application. i am just authenticating those values and returning the "http status codes and response" (using cfheader). For that I am trying the "RESTClient" in firefox extension. My problem is - It is running the "GET" method successfully but not the POST method.I guess the request is not even going to the server in POST method. See the screen shot below of both the request: 1. GET request 在此处输入图片说明

  1. POST request 在此处输入图片说明

This is my 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>

FYI a GET request has no request entity body because it is a GET request and the web server will generally ignore any body included in the request. I am surprised that ColdFusion provides a response since I don't see a GET method in your CFC.

The POST method is probably failing because the CFC is expecting a MIME type of application/form-url-encoded (your CFARGUMENTs) and you are posting application/json

Try breaking up into 2 functions, one which accepts application/json, one application/form-url-encoded

use the 'consumes' attribute in the CFFUNCTION to distinguish. They can both call a common function after this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM