rest - How to handle optional body with PUT or DELETE HTTP Requests (in a Play Application)? -


reservation resource 4 fields id, datacenter, startdate, enddate

i wish handle both types of requests:

  1. in there body list of datacenters

  2. in don't send body (in case perform operation on datacenters present in database).

without body:

put /reservation/123/end

delete /reservation/123

with body:

put /reservation/123/end

{     "datacenters": ["ams", "clt"] } 

delete /reservation

{     "datacenters": ["ams", "clt"] } 

the problem being server expects body valid json when there no body (which isn't case when body empty). best way handle use case?

i found 1 possible solution, not clean one, works.

for put

    json     .fromjson[optionaldatacenters](request.body.asjson.getorelse(jsarray()))     .getorelse(optionaldatacenters(none))     .datacenters 

for delete

action.async(parse.raw) { request =>   val datacenters =     request.body       .asbytes()       .map(         bytes =>           if (bytes.size > 1) json.parse(bytes.toarray).as[optionaldatacenters]           else optionaldatacenters(none)       )       .getorelse(optionaldatacenters(none))       .datacenters 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -