简体   繁体   中英

REST WEB API: How to make Content-Type header optional?

I have Web API and would like to make the content type optional.

Currently if I ignored content-Type header while calling REST (POST) Method it throws error.

This is how I defined route in Web API -

routes.MapHttpRoute(
   name: "myMethodPost",
   routeTemplate: "api/{id}/Settings/{settings}",
   defaults: new { controller = "Settings", action = "updateSettings" },
   constraints: new { httpMethod = new HttpMethodConstraint("POST") }
);

while calling this method -

api/1/Settings/testSettings
method = "Post"

it throws error. but when added following -

apiRequest.ContentType = "application/json";

it is working fine.

Now, how to make Content-Type header optional?

You could create a messagehandler on the server that will set the Content-Type to 'application/json' if it is missing and there is a non-zero length body. That way the client doesn't have to send it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM