Apigee选项404 [英] Apigee OPTIONS 404

查看:450
本文介绍了Apigee选项404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被Apigee的CORS支持所困扰。我设置了一个新代理,并确保勾选为您的API启用直接浏览器访问 - 允许通过CORS浏览器的直接请求。框。

I have been stumped by Apigee's CORS support. I setup a new proxy and made sure to tick the " Enable Direct Browser Access for Your API — Allow direct requests from a browser via CORS." box.

看起来CORS适用于普通的GET请求,但是没有找到飞行前的OPTIONS请求,并返回404。我发现这个 answer ,但无法解决我的问题,因为它似乎是一个不同的问题可能?

It appears that CORS is working for the normal GET requests, however pre-flight OPTIONS requests are not found and are returning a 404. I found this answer but was not able to resolve my problem because it seems like a different problem perhaps?

我想回答的主要问题是如何为所有请求设置Access-Control-Allow-Origin = *?甚至是OPTIONS请求?

The main question I would like answered is how do I setup Access-Control-Allow-Origin=* for all requests? Even OPTIONS requests?

代理端点

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ProxyEndpoint name="default">
    <Description/>
    <Flows>
        <Flow name="Forecast">
            <Description/>
            <Request/>
            <Response/>
            <Condition>(proxy.pathsuffix MatchesPath &quot;/forecast&quot;) and (request.verb = &quot;GET&quot;)</Condition>
        </Flow>
    </Flows>
    <PreFlow name="PreFlow">
        <Request/>
        <Response/>
    </PreFlow>
    <HTTPProxyConnection>
        <BasePath>/v1/weather</BasePath>
        <VirtualHost>default</VirtualHost>
        <VirtualHost>secure</VirtualHost>
    </HTTPProxyConnection>
    <RouteRule name="default">
        <TargetEndpoint>default</TargetEndpoint>
    </RouteRule>
    <PostFlow name="PostFlow">
        <Request/>
        <Response/>
    </PostFlow>
</ProxyEndpoint>

目标端点

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TargetEndpoint name="default">
    <Description/>
    <Flows>
        <Flow name="OptionsCORS">
            <Description/>
            <Request/>
            <Response>
                <Step>
                    <Name>CrossOriginResourceSharing</Name>
                </Step>
            </Response>
            <Condition>request.verb equals "OPTIONS"</Condition>
        </Flow>
    </Flows>
    <PreFlow name="PreFlow">
        <Request/>
        <Response>
            <Step>
                <Name>CrossOriginResourceSharing</Name>
            </Step>
        </Response>
    </PreFlow>
    <HTTPTargetConnection>
        <URL>https://home.nest.com/api/0.1/weather</URL>
    </HTTPTargetConnection>
    <PostFlow name="PostFlow">
        <Request/>
        <Response/>
    </PostFlow>
</TargetEndpoint>

添加CORS文件

<AssignMessage async="false" continueOnError="false" enabled="true" name="CrossOriginResourceSharing">
    <DisplayName>Add CORS</DisplayName>
    <FaultRules/>
    <Properties/>
    <Add>
        <Headers>
            <Header name="Access-Control-Allow-Origin">*</Header>
            <Header name="Access-Control-Allow-Headers">origin, x-requested-with, accept</Header>
            <Header name="Access-Control-Max-Age">3628800</Header>
            <Header name="Access-Control-Allow-Methods">GET, PUT, POST, DELETE, OPTIONS</Header>
        </Headers>
    </Add>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>

只是为了帮助 - 以下是我在请求时得到的错误。我使用Chrome和AngularJS应用程序。我已经能够使用cURL语句复制问题
(curl -HOrigin:localhost--verbose http:// * ** strong> -prod.apigee.net/v1/weather/forecast/12345 -X OPTIONS)

Just in case it helps- the following is the error I get when doing my request. I'm using Chrome and have an AngularJS app. I've been able to replicate the issue using a cURL statement as well ( curl -H "Origin: localhost" --verbose http://***-prod.apigee.net/v1/weather/forecast/12345 -X OPTIONS )

{
    "url": "/api/0.1/weather/forecast/73013",
    "message": "404 Not Found"
}

谢谢!

推荐答案

解决方案是添加一个RouteRule,请求从传递到我的API的OPTIONS请求。

The solution was to add a RouteRule that prevented the request from passing through to my API on OPTIONS requests.

<RouteRule name="NoRoute">
    <Condition>request.verb == "OPTIONS"</Condition>
</RouteRule>

此外,我添加了一个流程,将CORS支持添加到响应

Additionally I added a flow that added CORS support to the response

<Flow name="OptionsPreFlight">
    <Request/>
        <Response>
            <Step>
                <Name>Add-CORS</Name>
            </Step>
        </Response>
    <Condition>request.verb == "OPTIONS"</Condition>
</Flow>

和我的Final Add-CORS政策

And my Final Add-CORS policy

<AssignMessage async="false" continueOnError="false" enabled="true" name="Add-CORS">
    <DisplayName>Add CORS</DisplayName>
    <FaultRules/>
    <Properties/>
    <Add>
        <Headers>
            <Header name="Access-Control-Allow-Origin">*</Header>
            <Header name="Access-Control-Allow-Headers">origin, x-requested-with, accept</Header>
            <Header name="Access-Control-Max-Age">3628800</Header>
            <Header name="Access-Control-Allow-Methods">GET, PUT, POST, DELETE</Header>
        </Headers>
    </Add>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>

这篇关于Apigee选项404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆