rest - How to tell the client that the data type is unsupported -


writing restful api, i've stumbled on problem.
if client post/put data in format not supported api, how should server alert client?

e.g.: client posted data in querystring format

fielda=foo&fieldb=bar 

but server accepts json, data should posted this:

{"fielda": "foo", "fieldb": "bar"} 

currently, i'm sending 415 status code (php code):

header("http/1.1 415 unsupported media type", true, 415); 

but doesn't tell client media type api accepts/consumes.
besides, enough or the right way fail request?

the question is, in restful api, best way tell client data sent server in unsupported media type, , how server tells client supported/expected?

ps: i'm writing application in php i'm interested in behavior, not actual implementation.
because logic same everywhere, responses in language welcome.

first of need distinguish between these 2 types of errors:

  1. 400 bad request - not same as:
  2. 415 unsupported media type

when request unsupported media type sent 415 should returned. since there's no header indicates media types accepted endpoint , precise info needed, detailed message may returned in response's body. short, return 415 messages xml , json supported. in frameworks returned error can caught , wrapped. remember always return error message in same format consumer may parse it.

when request sent in valid format (json) e.g. single field sent instance of string , required integer, 400 should returned detailed message.

hope helps you.


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -