Handling invalid Resource Paths using a default Resource and the Raise Fault policy

"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://<org-name>-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:

1) 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.

2828-unknown.png

2) Change the Resource config:

523-screenshot-unknownresourceconfig1.png

Remove the <Condition>....</Condition> tags. Hit 'Save' (top left hand corner).

3) Add a Raise Fault policy:

Click on New Policy >> Raise Fault.

527-screenshot-unknownresourceconfig3.png

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.

4) 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.

528-screenshot-unknownresource-req.png

Comments
rdeandrade
Staff

Thanks Vidya,

2 more points to add:

- Use the Payload session in the RaiseFault policy to customize the response sent:

<RaiseFault async="false" continueOnError="false" enabled="true" name="404-Not-Found">
    <DisplayName>404 Not Found</DisplayName>
    <Properties/>
    <FaultResponse>
    <Set>
        <Headers/>
        <Payload contentType="application/json">\{"error":"Not Found", "code":404} </Payload>
        <StatusCode>404</StatusCode>
        <ReasonPhrase>Not Found</ReasonPhrase>
    </Set>
    </FaultResponse>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</RaiseFault>

- 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.

Not applicable

Please keep in mind that the screenshot is no longer valid for the existing version of Apigee.

You should chose "New Conditional Flow" on the Proxy Endpoints and specify this custom condition (pay attention to the *):

(proxy.pathsuffix MatchesPath "/*")
anilsr
Staff

@JeanPaulSmit , Welcome to Apigee Community 🙂

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.

Keep us posted if any moving forward.

Not applicable

Hi @Vidya Ravindran,

Can you share the revision of your proxy in which you have integrated this RaiseFault policy ?

soujanyaedunuri
Bronze 3
Bronze 3

(proxy.pathsuffix MatchesPath "/*") only matches one path element, and if the invalid url has more than one path element, then it will skip the flow.

So, either remove the condition for unknown resource flow (or) add the condition like

(proxy.pathsuffix MatchesPath "**")
soujanyaedunuri
Bronze 3
Bronze 3

@Anil Sagar, @Vidya Ravindran,

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 ?

Not applicable
@soujanyaedunuri

Did you try the below provided solution?

I have tried the below solution.It did not work for me?

Version history
Last update:
‎06-02-2015 03:44 PM
Updated by: