How can I send HTTP Status Code and a Response messages to the client in ColdFusion?

末鹿安然 提交于 2019-12-11 05:27:33

问题


I am implementing the Single Sign On functionality. I have an ColdFusion application which takes input parameters from Java application (POST request). What I need to do is return status codes and a description to indicate whether the user has access and the failed reason if the user does not have access to my CF application. Something like below:

I have created a cfc and provided this as an API to allow Java users to pass in their UserName, CustomerID to my CF application. Do I need to write the return response logic in the same file? Like a function which "throw" error code (cfthrow).

Or may be I can use "cfheader"....something like this:

<cfif form.CustomerId EQ queryname.CustID>
<CFHEADER 
    STATUSCODE="200"
    STATUSTEXT="Success">

<cfelse>
 <CFHEADER 
    STATUSCODE="400"
    STATUSTEXT="Insufficient Input">
</cfif>

Can anyone please help me here?


回答1:


You can use:

component restpath = "your/rest/path" rest="true"
{
  remote void function errorTest()
    httpmethod = "GET"
    restpath   = ""
  {
    cfheader(
      statuscode = 401,
      statustext = "Invalid Password"
    );

    // or

    restSetResponse({
      status = 401,
      headers = { explanation = "Invalid Password" }
    });

    // or, using Java

    getPageContext()
        .getResponse()
        .getResponse()
        .sendError( JavaCast( 'int', 401 ), "Invalid Password" );

    // or, using the deprecated setStatus(int,string) method in Java

    getPageContext()
        .getResponse()
        .getResponse()
        .setStatus( JavaCast( 'int', 401 ), "Invalid Password" );
  }
}

Note: I have not found a way to directly set the message using restSetResponse() so this returns a custom header with the message instead.



来源:https://stackoverflow.com/questions/45009300/how-can-i-send-http-status-code-and-a-response-messages-to-the-client-in-coldfus

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!