CORS error trying to consume __admin/mappings #396

Closed
opened 2025-12-29 15:22:41 +01:00 by adam · 9 comments
Owner

Originally created by @devmariodiaz on GitHub (Jan 5, 2022).

Hello Stef,

Happy new year and hoping that you are doing well.

I'm trying to consume __admin/mappings to create new mappings using the method/verb POST and an application developed on React, but I'm getting an error when I send any request. CORS error.

Screen Shot 2022-01-05 at 3 34 44 PM

Any clue what I can do to resolve that?

Originally created by @devmariodiaz on GitHub (Jan 5, 2022). Hello Stef, Happy new year and hoping that you are doing well. I'm trying to consume __admin/mappings to create new mappings using the method/verb POST and an application developed on React, but I'm getting an error when I send any request. CORS error. ![Screen Shot 2022-01-05 at 3 34 44 PM](https://user-images.githubusercontent.com/54611419/148286539-8ea7daa0-d9b6-4740-8a65-d68cc995fa0f.png) Any clue what I can do to resolve that?
adam added the question label 2025-12-29 15:22:41 +01:00
adam closed this issue 2025-12-29 15:22:41 +01:00
Author
Owner

@StefH commented on GitHub (Jan 6, 2022):

Hello @devmariodiaz,

CORS support is not enabled by default, you can enable it yourself when configuring WireMock Server, like:

/* https://stackoverflow.com/questions/31942037/how-to-enable-cors-in-asp-net-core */
            /* Enable Cors */
            settings.AdditionalServiceRegistration = services =>
            {
                services.AddCors(corsOptions =>
                    corsOptions.AddPolicy("MyPolicy",
                        corsPolicyBuilder =>
                        {
                            corsPolicyBuilder
                                .AllowAnyHeader()
                                .AllowAnyMethod()
                                .AllowAnyOrigin();
                        }));

                settings.Logger.Debug("Enable Cors");
            };

            /* Use Cors */
            settings.PreWireMockMiddlewareInit = app =>
            {
                var appBuilder = (IApplicationBuilder)app;
                appBuilder.UseCors();

                settings.Logger.Debug("Use Cors");
            };

            _server = WireMockServer.Start(settings);

See also https://github.com/WireMock-Net/WireMock.Net/blob/cors/examples/WireMock.Net.StandAlone.NETCoreApp/Program.cs#L38

@StefH commented on GitHub (Jan 6, 2022): Hello @devmariodiaz, CORS support is not enabled by default, you can enable it yourself when configuring WireMock Server, like: ``` c# /* https://stackoverflow.com/questions/31942037/how-to-enable-cors-in-asp-net-core */ /* Enable Cors */ settings.AdditionalServiceRegistration = services => { services.AddCors(corsOptions => corsOptions.AddPolicy("MyPolicy", corsPolicyBuilder => { corsPolicyBuilder .AllowAnyHeader() .AllowAnyMethod() .AllowAnyOrigin(); })); settings.Logger.Debug("Enable Cors"); }; /* Use Cors */ settings.PreWireMockMiddlewareInit = app => { var appBuilder = (IApplicationBuilder)app; appBuilder.UseCors(); settings.Logger.Debug("Use Cors"); }; _server = WireMockServer.Start(settings); ``` See also https://github.com/WireMock-Net/WireMock.Net/blob/cors/examples/WireMock.Net.StandAlone.NETCoreApp/Program.cs#L38
Author
Owner

@devmariodiaz commented on GitHub (Jan 6, 2022):

Thank you, Stef for your quick answer.

I tried to apply this configuration that you provided, but, it's still sending me the same error related to the CORS error.

We are using our app as a Standalone and as a Service, may be this be the problem?

Screen Shot 2022-01-06 at 7 09 03 AM
@devmariodiaz commented on GitHub (Jan 6, 2022): Thank you, Stef for your quick answer. I tried to apply this configuration that you provided, but, it's still sending me the same error related to the CORS error. We are using our app as a Standalone and as a Service, may be this be the problem? <img width="797" alt="Screen Shot 2022-01-06 at 7 09 03 AM" src="https://user-images.githubusercontent.com/54611419/148380957-1eac3061-df83-4f56-afde-2bb3e6fa29d2.png">
Author
Owner

@StefH commented on GitHub (Jan 6, 2022):

I forgot some code.

You need to use the same policy-name:

/* https://stackoverflow.com/questions/31942037/how-to-enable-cors-in-asp-net-core */
            /* Enable Cors */
            var policyName = "MyPolicy";
            settings.AdditionalServiceRegistration = services =>
            {
                services.AddCors(corsOptions =>
                    corsOptions.AddPolicy(policyName, // ◀️ MyPolicy
                        corsPolicyBuilder =>
                        {
                            corsPolicyBuilder
                                .AllowAnyHeader()
                                .AllowAnyMethod()
                                .AllowAnyOrigin();
                        }));

                settings.Logger.Debug("Enable Cors");
            };

            /* Use Cors */
            settings.PreWireMockMiddlewareInit = app =>
            {
                var appBuilder = (IApplicationBuilder)app;
                appBuilder.UseCors(policyName); // ◀️ MyPolicy

                settings.Logger.Debug("Use Cors");
            };

This works for me:
https://www.w3schools.com/xml/tryit.asp?filename=tryajax_get2
image

-->

image

calling __amdin/mappings also works fine:
image

@StefH commented on GitHub (Jan 6, 2022): I forgot some code. You need to use the same policy-name: ``` c# /* https://stackoverflow.com/questions/31942037/how-to-enable-cors-in-asp-net-core */ /* Enable Cors */ var policyName = "MyPolicy"; settings.AdditionalServiceRegistration = services => { services.AddCors(corsOptions => corsOptions.AddPolicy(policyName, // ◀️ MyPolicy corsPolicyBuilder => { corsPolicyBuilder .AllowAnyHeader() .AllowAnyMethod() .AllowAnyOrigin(); })); settings.Logger.Debug("Enable Cors"); }; /* Use Cors */ settings.PreWireMockMiddlewareInit = app => { var appBuilder = (IApplicationBuilder)app; appBuilder.UseCors(policyName); // ◀️ MyPolicy settings.Logger.Debug("Use Cors"); }; ``` This works for me: https://www.w3schools.com/xml/tryit.asp?filename=tryajax_get2 ![image](https://user-images.githubusercontent.com/249938/148388805-99a22094-2fd5-4e8f-9399-431eec039ece.png) --> ![image](https://user-images.githubusercontent.com/249938/148388833-8b93ad1c-d638-482e-a019-fe32be78552a.png) calling `__amdin/mappings` also works fine: ![image](https://user-images.githubusercontent.com/249938/148389059-632b582c-6a44-4627-9965-b704cb5fb0a6.png)
Author
Owner

@devmariodiaz commented on GitHub (Jan 6, 2022):

Thanks a lot for your help, Stef,

Now is working fine for me.

@devmariodiaz commented on GitHub (Jan 6, 2022): Thanks a lot for your help, Stef, Now is working fine for me.
Author
Owner

@StefH commented on GitHub (Jan 6, 2022):

https://github.com/WireMock-Net/WireMock.Net/wiki/Cors

@StefH commented on GitHub (Jan 6, 2022): https://github.com/WireMock-Net/WireMock.Net/wiki/Cors
Author
Owner

@StefH commented on GitHub (Jan 12, 2022):

@devmariodiaz
I decided to add Cors support in WireMock.Net server by using a setting CorsPolicyOptions.

Can you try preview (1.4.31-ci-15770) ?

var settings = new WireMockServerSettings
{
   CorsPolicyOptions = CorsPolicyOptions.AllowAll
};

@StefH commented on GitHub (Jan 12, 2022): @devmariodiaz I decided to add Cors support in WireMock.Net server by using a setting `CorsPolicyOptions`. Can you try preview (1.4.31-ci-15770) ? ``` c# var settings = new WireMockServerSettings { CorsPolicyOptions = CorsPolicyOptions.AllowAll }; ```
Author
Owner

@devmariodiaz commented on GitHub (Jan 12, 2022):

Thanks a lot Stef,

I will test this preview ASAP, I believe this is a great decision.

@devmariodiaz commented on GitHub (Jan 12, 2022): Thanks a lot Stef, I will test this preview ASAP, I believe this is a great decision.
Author
Owner

@StefH commented on GitHub (Jan 15, 2022):

@devmariodiaz
Did you have time to verify?

@StefH commented on GitHub (Jan 15, 2022): @devmariodiaz Did you have time to verify?
Author
Owner

@StefH commented on GitHub (Jan 24, 2022):

https://github.com/WireMock-Net/WireMock.Net/pull/714

@StefH commented on GitHub (Jan 24, 2022): https://github.com/WireMock-Net/WireMock.Net/pull/714
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#396