Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two calls on Post request: with http 204 and 200

I have implemented Cors policy in dot net core application: In Startup.cs under ConfigureServices I have added the following cors policy

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

I'm facing a strange issue after adding CORS policy, on every POST call from UI there are two calls made: first calls returns with 204 and other call returns the data with 200 status code.

two calls on post request

like image 648
Nikitesh Avatar asked Aug 07 '17 09:08

Nikitesh


People also ask

What is the difference between 200 and 204 status code?

The 204 (No Content) status code indicates that the server has successfully fulfilled the request and that there is no additional content to send in the response payload body. While 200 OK being a valid and the most common answer, returning a 204 No Content could make sense as there is absolutely nothing to return.

Can a post request return 204?

From the service's perspective, a 204 (No Content) response may be a perfectly valid response to a POST, PUT or DELETE request.

Can a post request return 200?

In all cases you return 200 because the operation was successful. There is no need for a different status code if nothing could be found to match my search term. If still in doubt, take it by process of elimination (list of HTTP status codes): 1xx informational response – the request was received, continuing process.

How do I fix status code 204?

By default, 204 (No Content) the response is cacheable. If caching needs to be overridden then the response must include cache respective cache headers. For example, you may want to return status 204 (No Content) in UPDATE operations where request payload is large enough not to transport back and forth.


1 Answers

First one is a preflighted request. The main goal is to determinate whether the actual request is safe to send. Cross-site requests are preflighted since they may have implications to user data.

A CORS preflight request is a CORS request that checks to see if the CORS protocol is understood.

It is an OPTIONS request using two HTTP request headers: Access-Control-Request-Method and Access-Control-Request-Headers, and the Origin header.

A preflight request is automatically issued by a browser when needed.

This HTTP access control (CORS) describe conditions that if true then request is preflighted.

like image 91
Set Avatar answered Oct 23 '22 06:10

Set