Asked by:
Quality factor Accept: text/plain; q=0.5, text/html

Question
-
User-1960118997 posted
Please help me understand how and where Web API server process value of q onj server while deciding upon the response format based on the Accept header it received in request
Wednesday, July 12, 2017 12:32 PM
All replies
-
User1068175894 posted
you can manage that via config.Formatters in AppStart\WebApiConfig.cs
depending on what you want you can do things like:
//Needed to add the following line in order to return XML or else it always returns JSON. GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseDataContractSerializer = true;
or
config.Formatters.JsonFormatter.SupportedMediaTypes .Add(new MediaTypeHeaderValue("text/html") );
to return json on most queries, but still get xml when you send text/xml
Wednesday, July 12, 2017 1:08 PM -
User753101303 posted
Hi,
As often if you need further help, it will be likely easier to help if you explain what you want to do. You want to serve text/plain or text/html in response to a web api call ? It has to be a web api call rather than a MVC view?
Wednesday, July 12, 2017 1:17 PM -
User-1220080607 posted
Hi,
In web api pipeline while returning respose " IContentNegotiator " checks Accept header in request for in which format to send the response to the client.
Here below i'm giving sample and "Accept" header and Quality Factor :-
This is my Sample controller
[RoutePrefix("MotorBike")] public class MotorsController : ApiController { [Route("")] [HttpGet] public IHttpActionResult GetBikeCollection() { MotroLogic logic = new MotroLogic(); return Ok(logic.GetBikesAndShowRoom()); } }
Now use fidler or PostMan for to test api.Heare i'm using fidler
1.First i'm sending Accept Header as Json as follow
My Http Request Header as follows GET /MotorBike HTTP/1.1 Host: localhost:3845 Accept: application/json Cache-Control: no-cache for this request my response in json format,because my Accept Header is Json {"Bikes":[{"Id":1,"BikeName":"Royal Enfield","Cost":100000.00},{"Id":2,"BikeName":"Harley Davidson","Cost":200000.00},{"Id":3,"BikeName":"Hero
","Cost":300000.00}],"ShowRooms":[{"Id":1,"Name":"Viswash"},{"Id":2,"Name":"NiSta"}]}2. I'm sending Accept Header as Xml
My request Header GET /MotorBike HTTP/1.1 Host: localhost:3845 Accept: application/xml Cache-Control: no-cache Result:-xml <MotorResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/ApiSample.DTO">
<Bikes xmlns:d2p1="http://schemas.datacontract.org/2004/07/ApiSample.Models"><d2p1:Bikes><d2p1:BikeName>Royal Enfield</d2p1:BikeName>
<d2p1:Cost>100000.00</d2p1:Cost><d2p1:Id>1</d2p1:Id></d2p1:Bikes><d2p1:Bikes><d2p1:BikeName>Harley Davidson</d2p1:BikeName>
<d2p1:Cost>200000.00</d2p1:Cost><d2p1:Id>2</d2p1:Id></d2p1:Bikes><d2p1:Bikes><d2p1:BikeName>Hero </d2p1:BikeName><d2p1:Cost>300000.00</d2p1:Cost><d2p1:Id>3</d2p1:Id></d2p1:Bikes></Bikes><ShowRooms xmlns:d2p1="http://schemas.datacontract.org/2004/07/ApiSample.Models"><d2p1:BikeShowRooms><d2p1:Id>1</d2p1:Id><d2p1:Name>Viswash</d2p1:Name></d2p1:BikeShowRooms><d2p1:BikeShowRooms><d2p1:Id>2</d2p1:Id><d2p1:Name>NiSta</d2p1:Name></d2p1:BikeShowRooms></ShowRooms></MotorResult>3.Above two cases single Accept Header was send, now i'm sending two Accept header xml and Json. When ever api founds more than one matching Accept header in it Formater Dictionary Web api gives Priority to the Accept Header with Maximum Quality factor Value.
Note:-Range of this Quality Factor is integer including decimal values
(a)Below Sample i'm Giving Xml with maximum Quality factor Value and then response will be xml format
Requset Header GET /MotorBike HTTP/1.1 Host: localhost:3845 Accept: application/xml;q=2,application/json;q=1 Cache-Control: no-cache Here Xml Qualit value 2 greater than Json Quality factor value Result: <MotorResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/ApiSample.DTO">
<Bikes xmlns:d2p1="http://schemas.datacontract.org/2004/07/ApiSample.Models">
<d2p1:Bikes><d2p1:BikeName>Royal Enfield</d2p1:BikeName><d2p1:Cost>100000.00</d2p1:Cost><d2p1:Id>1</d2p1:Id>
</d2p1:Bikes><d2p1:Baikes><d2p1:BikeName>Harley Davidson</d2p1:BikeName><d2p1:Cost>200000.00</d2p1:Cost><d2p1:Id>2</d2p1:Id></d2p1:Bikes><d2p1:Bikes><d2p1:BikeName>Hero </d2p1:BikeName><d2p1:Cost>300000.00</d2p1:Cost><d2p1:Id>3</d2p1:Id></d2p1:Bikes></Bikes><ShowRooms xmlns:d2p1="http://schemas.datacontract.org/2004/07/ApiSample.Models"><d2p1:BikeShowRooms><d2p1:Id>1</d2p1:Id><d2p1:Name>Viswash</d2p1:Name></d2p1:BikeShowRooms><d2p1:BikeShowRooms><d2p1:Id>2</d2p1:Id><d2p1:Name>NiSta</d2p1:Name></d2p1:BikeShowRooms></ShowRooms></MotorResult>(b)Now i'm giving Json Accepti Header with max Quality Factor value then response in Json
GET /MotorBike HTTP/1.1 Host: localhost:3845 Accept: application/xml;q=3,application/json;q=4 Cache-Control: no-cache Result :- {"Bikes":[{"Id":1,"BikeName":"Royal Enfield","Cost":100000.00},{"Id":2,"BikeName":"Harley Davidson","Cost":200000.00},
{"Id":3,"BikeName":"Hero ","Cost":300000.00}],"ShowRooms":[{"Id":1,"Name":"Viswash"},{"Id":2,"Name":"NiSta"}]}I hope this will help you
Thanks
Wednesday, July 12, 2017 5:35 PM