IIS - Rewrite Module from 200 OK to 201 Accepted

Dimitrios Michas 0 Reputation points
2024-01-22T19:43:55.25+00:00

In IIS version 10, I installed Rewrite Module and I am trying with the above XMLs the following action.
I want to set "201 or 202 Active" status instead of default 200 OK to all successful requests (only when my api should return 200 OK). When it matches 200 should return 201 or 202 (it works fine), but it returns 202 to all of the requests i send via postman, even when it should return 400.
I am trying to find how this will work but I keep getting 202 for all requests.

How can I write/configure it in IIS without changing my api ?

<rule name="SetStatusTo202ForSuccess" enabled="false" patternSyntax="Wildcard" stopProcessing="true">
<match url="item-base-data" />
 <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
  <add input="{RESPONSE_STATUS}" pattern="200" />
 </conditions>
<action type="CustomResponse" statusCode="202" subStatusCode="200" statusReason="Accepted" statusDescription="." />
<serverVariables>
  <set name="RESPONSE_STATUS" value="202" />
</serverVariables>
</rule>


I tried, inbound rule and outbound rules.
In outbound rules I tried this :

<rule name="ChangeStatusCode" preCondition="Is200StatusCode" patternSyntax="Wildcard">
<match serverVariable="{RESPONSE_STATUS}" pattern="200" />
<action type="Rewrite" value="202" />
</rule>
<preConditions>
<preCondition name="Is200StatusCode">
<add input="{RESPONSE_STATUS}" pattern="200" />
</preCondition>
</preConditions>

Thank you in advance.

Internet Information Services
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,509 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
343 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sam Wu-MSFT 7,531 Reputation points Microsoft Vendor
    2024-01-23T03:05:20.66+00:00

    @Dimitrios Michas

    Your rule will return 202 to all requests, which means that all requests match this rule, so what you need to change is the matching parameter in the rule so that it only matches 200. When the request returns 400, the matching condition should not match.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.