“How can I restrict an incoming request with mis-spelled resource URI from being forwarded to the back-end? Currently if I make a call like http://-test.apigee.net/v1/weather/forecastrssXXX?w=12795287 the request is not stopped at Apigee. It is forwarded to Yahoo which responds back and shows a not-found page. Ideally I should be able to stop the request right at my proxy from going further since it had an incorrect resource URI.”
One way to fix this is by adding a proxy Resource that handles all invalid resource paths by raising a fault and returning an error response, without sending the request to the backend, when an unknown request url is called. Here’s how:
Add new proxy resource:
Under the API Proxy Development view, click on + Icon Next to Default in Proxy EndPoints to add a conditional flow.
For now, specify Condition Type as “Path”, and Resource Path as “/”. We’ll remove this later.
Once added, change the policy config to the following:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><RaiseFaultasync="false"continueOnError="false"enabled="true"name="Raise-Fault-Unknown-Resource"><DisplayName>Raise Fault - Unknown Resource</DisplayName><FaultResponse><Set><StatusCode>404</StatusCode><ReasonPhrase>Resource not found.</ReasonPhrase></Set></FaultResponse><IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables></RaiseFault>
Hit Save. Deploy to environment of choice.
Now, every time a request comes in with an unknown URL path, the proxy will attempt to match with all known Resources, and finally with the Unknown Resource flow. Since we have removed any conditional config for this resource, it will match it be default if no others match.
The proxy will then Raise the Fault for Unknown Resource Path, returning a HTTP 404.
Make sure the resource with no condition is always the last in the flow, otherwise it’ll be always executed as the ‘condition’ matches. Any other flows added after this resource would need to be moved above it in the XML.
Thank you for highlighting this. Yes, Things have changed in the recent version where you add conditional flow. I have update the screenshot & text. Thank you once again for reporting this.
If we have steps in PreFlow ( ex: Verifying apikey or verifying aouth token), these steps get executed before executing conditional flow for Unknown resource path.
What is the better approach for handling unknown resource paths if there are steps to be executed in preflow ?