Hoss
Hoss

Reputation: 942

RESTFul: How should a POST request body format be specified?

I'm working a RESTFul API and have a POST method that will allow either JSON or CSV body content.

What is best way to handle this? Should URL routing/parameters be used (such as /resource/json or /resource/?type=json), or through headers (so URL would just be /resource and a header would specify json or csv)?

Upvotes: 1

Views: 3277

Answers (1)

Ray
Ray

Reputation: 41428

The proper way according to W3C (https://www.w3.org/Protocols/rfc1341/4_Content-Type.html) is to use the Content Type header to specify the correct type.

  • For json it would be Content-Type: application/json
  • For a text CSV it would be Content-Type: text/csv

Avoid using url params. While this is not 'non-restful' it's not the recommended method and smells of amateur hour. Even worse is creating a new 'resource' which is just a representation of an existing resource.

Upvotes: 1

Related Questions