I'm not sure this actually qualifies as a ColdFusi...
# cfml-general
t
I'm not sure this actually qualifies as a ColdFusion question exactly -- it's more of an IIS question really. But I'm making an endpoint for an API. I would like to return a JSON string that gives the status of the request. The code itself is, I think, fine:
Copy code
httpData = {
	"statusCode": statusCode,
	"statusText": statusText
};
cfheader(statuscode: statusCode, statustext: statusText);
cfheader(name: "Content-Type", value: "application/json");
WriteOutput(SerializeJSON(httpData));
And when the response is a 200, I do get JSON output. But on an error (412, for example), I'm not getting
Copy code
{
  "statusCode": 412,
  "statusText": "My error message with what's missing"
}
Instead I'm getting the default IIS 412 page, which is HTML.
googling is suggesting I set this property in my ASP.NET code (of which, I obviously have none...)
Response.TrySkipIisCustomErrors = true;
So that's not very helpful.
p
In your IIS settings you have to modify your web.config file to deal with the various error codes; sounds like your default IIS config is just picking them all up.
"response codes" I meant
PS- every site using IIS has a web.config file that they are prob referring to
t
I'm familiar with the web.config files, and I already have this one for the page's directory
Copy code
<?xml version="1.0" encoding="utf-8"?>
<configuration>
	<system.webServer>
		<httpErrors existingResponse="PassThrough"/>
	</system.webServer>
</configuration>
But it doesn't appear to be passing my responses through like I expected.
j
In my experience, you have to have the PassThrough, but you also need to specify
<remove statusCode="412" />
within the
<httpErrors />
p
yep he beat me to it ^
t
thanks. I'll give that a try. I assume I'd need to remove everything I'm expecting to return?
j
Anything you will be handling manually, yes.
p
Also, if this isnt working for you, because by default it should have; you may need to restart your application pool for the site.
t
boo. I don't have access to that...
but we'll see what happens. Fingers crossed it'll be fine after the removes.
yeah, it's not working. I guess I'll put in a ticket to central it to restart the app pool.
sigh
Central IT restarted IIS this morning, and it's still not working. My web.config is now:
Copy code
<?xml version="1.0" encoding="utf-8"?>
<configuration>
	<system.webServer>
		<httpErrors existingResponse="PassThrough">
			<remove statusCode="401"/>
			<remove statusCode="412"/>
			<remove statusCode="424"/>
		</httpErrors>
	</system.webServer>
</configuration>
Did I screw anything up there?
p
Possible to add in this to possibly disable customErrors from IIS:
Copy code
<system.web>
    <customErrors mode="Off"/>
</system.web>
Note that goes outside of the
<system.webServer>
group
t
I spun up a server that I could play around in, and if I put the above web.config file in the site root, it works, and I get the output I expect. But if I put it in the directory with my endpoint, then it doesn't do anything.
p
web.config goes in the site's root
I believe sub-ones work but its been a long time on recalling that
t
they're definitely supposed to. Like, if I go into a sub directory, and set things through the GUI, IIS creates a web.config file in that directory to reflect the config changes I made.
It just doesn't appear to be following them.
p
Well at the top server level the host/server admin ppl could have configuration rules etc on how they are handled