Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GET request - desired response format in parameter or Accept header

I am working on an ASP.Net Web API 2 RESTful web service for importing and exporting data from my database. For my export method, the client has several options for exporting data in different formats (e.g. pdf, xml, etc.). I am trying to determine the best design for my interface for the client to inform the service which format to provide the data in.

As far as I can tell, my 2 best options are to use the Accept Header with media type strings or adding a parameter to the method where the client can provide the format parameter in the query string. If I choose to use the Accept Header this may involve using custom media type strings.

It seems to me that using the Accept Header would be more inline with HTTP standards and RESTful practices, but using the format parameter in the query string would be easier to implement on the service side and for the client.

Can anyone explain what some of the advantages or disadvantages of these 2 designs are?

like image 451
Brian Avatar asked Oct 20 '22 22:10

Brian


1 Answers

Both your approaches are valid ways to get to the same result. REST isn't a spec to implement, so whatever response you get here is likely to be someone's preferred way of doing it or how they've understood REST.

Your question is similar to this one from programmers.SE: Tradeoffs between content negotiation via Accept header versus extensions. Do note also that pdf, xml etc have standard recognized mime types so I don't see the need for custom media type strings.

That being said, my preferred way of GET-ing the report would be to have a ?format=pdf query string. Go with whatever is easier, more maintainable, cleaner, etc.

like image 89
Bogdan Avatar answered Oct 27 '22 22:10

Bogdan