Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have REST API, that is hosted in Azure. If I make request in interactive console with GET method ('/api/pets'), request goes through just fine. But when I make POST request (POST '/api/pets'), CORS error appears.

Response in interacive console throws this error: Response in interacive console throws this error

startup.cs

        public void ConfigureServices(IServiceCollection services)
        {
        
            .
            .
            .

            services.AddCors(options => options.AddPolicy("CorsPolicy",
                builder =>
                {
                    builder
                        .AllowAnyOrigin()
                        .AllowAnyHeader()
                        .AllowAnyMethod();
                }));
            .
            .
            .
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {

            app.UseCors("CorsPolicy");
            .
            .
            .
        }

All API's CORS policy

<policies>
    <inbound>
        <cors>
            <allowed-origins>
                <origin>*</origin>
            </allowed-origins>
            <allowed-methods preflight-result-max-age="300">
                <method>GET</method>
                <method>POST</method>
                <method>PUT</method>
                <method>DELETE</method>
                <method>HEAD</method>
                <method>OPTIONS</method>
                <method>PATCH</method>
                <method>TRACE</method>
            </allowed-methods>
            <allowed-headers>
                <header>*</header>
            </allowed-headers>
            <expose-headers>
                <header>*</header>
            </expose-headers>
        </cors>
    </inbound>
    <backend>
        <forward-request />
    </backend>
    <outbound />
    <on-error />
</policies>

I also tried setting CORS policy on azure portal only in specific API, but it doesn't work Everything works fine locally.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
3.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

Can you try configure this using the portal?

  1. Open your API in the Azure API Management section of the Azure portal
  2. Select All operations, or a single operation
  3. On the right, choose Inbound processing > Add policy
  4. You will get a list of prefab policy templates. Choose the "CORS" one and configure it at will:

UPDATE:

Can you modify the XML as below

 <cors>
     <allowed-origins>
        <origin>*</origin>
     </allowed-origins>
     <allowed-methods>
        <method>*</method>
      </allowed-methods>
         <allowed-headers>
      <header>*</header>
         </allowed-headers>
       <expose-headers>
           <header>*</header>
       </expose-headers>
  </cors>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...