I’m trying to make a request to my API that is hosted on a server from Swagger documentation, but all POST, PUT, DELETE, and PATCH methods are returning the following error:
Although GET methods are working properly without any error, and also all methods are working fine on localhost.
I’m using the following CORS policy in my code:
builder.Services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));
Also, I’m using the following code to add Swagger, considering that the API is hosted on a subfolder called /api
on the server:
if (app.Environment.IsProduction())
{
app.UseSwagger(options =>
{
options.PreSerializeFilters.Add((doc, request) =>
{
doc.Servers = new List<OpenApiServer>
{
new OpenApiServer { Url = $"{request.Scheme}://{request.Host.Value}/api" }
};
});
});
app.UseSwaggerUI();
}
else
{
app.UseSwagger();
app.UseSwaggerUI();
}