Don't call target endpoint if resource doesn't exist

How can I stop my proxy from forwarding a request on to the target endpoint if the request doesn't match on one of the proxy endpoints? I'd like it to return a 404 response.

Solved Solved
0 1 334
1 ACCEPTED SOLUTION

@Daniel Hoenig

You can include a default flow that doesn't have any condition and add a raise fault policy to the default flow to return the HTTP 404 error response

<Flows>
   <Flow name="ApiRoutesGet">
      <Description />
      <Request>
         <Step>
            <FaultRules />
         </Step>
      </Request>
      <Response />
  <Condition>(proxy.pathsuffix MatchesPath "/api/routes") and (request.verb = "GET")</Condition>
   </Flow>
   <Flow name="UnknownResource">
      <Description />
      <Request>
         <Step>
            <FaultRules />
            <Name>Unknown-Resource</Name>
         </Step>
      </Request>
      <Response />
   </Flow>
</Flows>
<RaiseFault async="false" continueOnError="false" enabled="true" name="Unknown-Resource">
  <DisplayName>Unknown-Resource</DisplayName>
  <FaultRules />
  <Properties />
  <FaultResponse>
    <Set>
      <Headers />
      <Payload contentType="application/json">{"error":{
        "code": "404",
        "message": "Unknown Resource"
    }}</Payload>
      <StatusCode>404</StatusCode>
      <ReasonPhrase>Not Found</ReasonPhrase>
    </Set>
  </FaultResponse>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</RaiseFault>

View solution in original post

1 REPLY 1

@Daniel Hoenig

You can include a default flow that doesn't have any condition and add a raise fault policy to the default flow to return the HTTP 404 error response

<Flows>
   <Flow name="ApiRoutesGet">
      <Description />
      <Request>
         <Step>
            <FaultRules />
         </Step>
      </Request>
      <Response />
  <Condition>(proxy.pathsuffix MatchesPath "/api/routes") and (request.verb = "GET")</Condition>
   </Flow>
   <Flow name="UnknownResource">
      <Description />
      <Request>
         <Step>
            <FaultRules />
            <Name>Unknown-Resource</Name>
         </Step>
      </Request>
      <Response />
   </Flow>
</Flows>
<RaiseFault async="false" continueOnError="false" enabled="true" name="Unknown-Resource">
  <DisplayName>Unknown-Resource</DisplayName>
  <FaultRules />
  <Properties />
  <FaultResponse>
    <Set>
      <Headers />
      <Payload contentType="application/json">{"error":{
        "code": "404",
        "message": "Unknown Resource"
    }}</Payload>
      <StatusCode>404</StatusCode>
      <ReasonPhrase>Not Found</ReasonPhrase>
    </Set>
  </FaultResponse>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</RaiseFault>