5

I have a REST service that either outputs JSON or returns a 404, depending on whether there's a match. In my existing CF11 setup, I can generate a 404 with

<cfthrow errorcode="404">

but this apparently does not work in CF2018 anymore. In order to generate a 404, I need to use Java (as directed here: How can I send HTTP Status Code and a Response messages to the client in ColdFusion?):

<cfscript>
    getPageContext()
    .getResponse()
    .getResponse()
    .sendError( JavaCast( 'int', 404 ), "" );
</cfscript>

This works, but this got me curious, so I created a little test page (not a REST service):

<cfparam name="URL.method" default="cfthrow" >
<cfif URL.method IS "cfthrow">
    <cftry>
        <cfthrow errorcode="404">
        <cfcatch></cfcatch>
    </cftry>
</cfif>
<cfif URL.method IS "cfheader">
    <cfheader statuscode="404">
</cfif>
<cfif URL.method IS "java">
    <cfscript>
        getPageContext()
            .getResponse()
            .getResponse()
            .sendError( JavaCast( 'int', 404 ), "" );
    </cfscript>
</cfif>

I can't get cfthrow to work here, either. But cfheader DOES work, and as expected, the Java one works, too. Can anyone explain why there are all these disparities? FYI, I'm running Win2012/IIS. Thanks.

p.s. With my existing REST service on CF11, I never could get

<cfheader statuscode="404">

to work, either, to generate a 404. In all cases when I say the 404 doesn't work, what returns is a blank page.

Sung
  • 480
  • 2
  • 11
  • 1
    If you are expecting `` to generate an HTTP status code of 404, that will never work in any version of CF. `cfthrow` just throws a trappable error. If that was working in your previous code base then look in your error handling routines. That must have been looking for a `404` errorcode and then using `cfheader` to actually return the HTTP status code. That should still work in CF 2018. If it's not, check your server configuration to verify that you are returning HTTP status codes. – Miguel-F Jun 19 '19 at 14:23
  • Thanks, Miguel-F. Looking at my very simple REST service on CF11, that's all I'm doing -- cfthrow errorcode="404". There's nary a cfheader or anything of its ilk. Any idea why cfheader does not work in either CF11 or CF2018 in my case? Like I said, I just get a blank page. The code couldn't be any simpler: ` { "column": [ #data# ] } ` – Sung Jun 19 '19 at 19:56
  • Does it work if you add the statustext attribute? Like this ``. My other thought is that you need to enable `Enable HTTP Status Codes` in the ColdFusion administrator. – Miguel-F Jun 19 '19 at 20:48
  • 1
    Also your webserver may need to be configured to allow the statuscode to be set from ColdFusion. For example, in IIS you need to enable PassThrough for the response. Like this `` – Miguel-F Jun 19 '19 at 20:54
  • I updated the "Enable HTTP Status Codes" but it had no effect. Keep in mind that CFHEADER does work just fine outside of REST -- it only doesn't work for REST. – Sung Jun 19 '19 at 21:33
  • Ah okay, I misunderstood then. So how are you checking for 404 on REST service? The page content on a 404 is blank. – Miguel-F Jun 20 '19 at 00:19
  • So with the java method, I get an IIS 404 -- You know, gray bar up top that says Server Error, and the box and inside the box, "404 - File or directory not found. The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable." With the CFHEADER or CFTHROW, I just get a blank page. You make a good point, though -- what if it's returning blank AND is actually a 404? But no. I just ran a test via Fiddler and via CFHEADER, the REST service returns a blank page that is an HTTP 200. So either this is a bug or I'm doing something wrong. – Sung Jun 20 '19 at 16:30

0 Answers0