0

I'm working on a website(Angular 8 .NET Core 3.0) that takes a url and tries to fetch the og tags from the page. I referred to the first answer on stackoverflow

Startup.cs

--ConfigureServices    
services.AddCors(o => o.AddPolicy("CORSPolicy", builder =>
            {
                builder.AllowAnyOrigin()
                       .AllowAnyMethod()
                       .AllowAnyHeader();
            }));
--Configure
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseCors("CORSPolicy");
        app.UseStaticFiles();
        if (!env.IsDevelopment())
        {
            app.UseSpaStaticFiles();
        }

        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller}/{action=Index}/{id?}");
        });

        app.UseSpa(spa =>
        {
            spa.Options.SourcePath = "ClientApp";

            if (env.IsDevelopment())
            {
                spa.UseAngularCliServer(npmScript: "start");
            }
        });
    }

Now I do a get request using below call.

this.httpCli.get(this.URL, { headers: headers }).subscribe(result => {
        console.log(result);
    }, error => {
            console.error(error.text);
            console.error(error);
    });

I still get

blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

for some urls. Because the output is HTML, i get HttpErrorResponse but with status ok and error text having the webpage source.Console Screenshot I need the HTML source to pull the og meta tags from .

How do I fix both these issues?

Gowtham M
  • 75
  • 1
  • 8

1 Answers1

1

You are getting html as a response from the endpoint. but it should be json response. so mostly you are getting error response from server as html. first check this url is correct in postman or alike.

prisar
  • 3,041
  • 2
  • 26
  • 27