Anil Purswani
Anil Purswani

Reputation: 1887

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?

Upvotes: 2

Views: 2998

Answers (1)

Darrel Miller
Darrel Miller

Reputation: 142024

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.

Upvotes: 2

Related Questions