3
votes

I have a .net core web app (api) running in IIS being called from an Angular2 app. The http call is making an options request call first as I'm setting the content-type custom header on a POST call. IIS (I assume) is returning the 415 (unsupported media type) on the options call.

Where do I need to set supported media types in IIS (or .net core)?

Client request:

let postparams = {
  Action: 'DoStuff'
};
let body = JSON.stringify(postparams);

var headers = new Headers();
headers.append('Accept','application/json');
headers.append('Content-Type','application/json');
let options = new RequestOptions({headers: headers});

var url = 'http://localhost:9056/api/resolve';
var response = this.http.post(url, body, options).map(res => res.json());

Options request from fiddler generated by Angular (returns 415):

OPTIONS http://localhost:9056/api/resolve HTTP/1.1
Host: localhost:9056
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://localhost:8100
User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Mobile Safari/537.36
Access-Control-Request-Headers: content-type
Accept: */*
Referer: http://localhost:8100/
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-GB,en;q=0.8

POST request that works in fiddler (returns 200):

POST http://localhost:9056/api/resolve HTTP/1.1
Host: localhost:9056
Connection: keep-alive
Content-Length: 26
Origin: http://localhost:8100
User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Mobile Safari/537.36
content-type: application/json
Accept: */*
Referer: http://localhost:8100/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en;q=0.8

{"Action":"DoStuff"}

If I set the content-type to text/plain then I get a 415 returned - do I need to set up in IIS or .net core that the return type is application/json somehow?

Thanks in advance community!

1

1 Answers

2
votes

Ok, so my problem in the end was around CORS. I was setting the CORS headers via IIS but setting them in .net core Startup resolved my problem. The Unsupported Media Type response code was throwing me off...

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddApplicationInsightsTelemetry(Configuration);

        // Needed to add this section, and....
        services.AddCors(options =>
        {
            options.AddPolicy("CorsPolicy",
                builder => builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials());
        });

        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        // ..... this line
        app.UseCors("CorsPolicy");

        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        app.UseApplicationInsightsRequestTelemetry();

        app.UseApplicationInsightsExceptionTelemetry();

        app.UseMvc();
    }

If I can save someone else some head scratching then happy days!