Expose REST: HTTPRequestHandler: SetStatusCode: 401

When you expose a REST web service and set the HTTPS status code to 401, it outputs the html below.
This is not a valid JSON response. how do I fix this?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
        <title>Some custom messenge.</title>
        <style type="text/css">
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;} 
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
#content{margin:0 0 0 2%;position:relative;}
        <div id="header">
            <h1>Server Error</h1>
        <div id="content">
            <div class="content-container">
                    <h2>Some custom message</h2>
                    <h3>You do not have permission to view this directory or page using the credentials that you supplied.</h3>
Hi Robert,

Having a Déjà vu here. Linking to your own thread from 4 months ago.

João Rosado
Joao, thanks again :) yes deja vu indeed, I wanted to build it to REST standard and again ran into same problem - are there any other issues that I should know about for other http status code?
As far as I know the 401 is the only one that currectly gives problems in the .NET stack.

In JAVA that one works, but there are some others that that are not recognized. Here is link for the Jersey documentation (the library used in JAVA for REST): https://jersey.java.net/apidocs/1.18/jersey/javax/ws/rs/core/Response.Status.html

João Rosado


We have the same issue with a web hook integration where IIS 401 is returning unwanted information in the body.

Is there still no workaround for IIS 401 status codes?