0

I want to update file on server.User can enter data and send request to server to update file content.For non empty string following code works but when user enter empty string i get exception.

angularJS code


$http.put("app/rest/updateMsg",fileContent)

Spring controller code

            @RequestMapping(value = "/updateMsg", method = RequestMethod.PUT)
    public ResponseEntity<Boolean> updateMsg(@RequestBody String fileContent){

         //update file code

    }

I found workaround for this . I explicitly set filecontent to " " when it is empty("").

Any other solution for this ?

sar
  • 1,277
  • 3
  • 21
  • 48
  • According to rest convention, there should always be a request body for PUTs – nikjohn Sep 02 '16 at 09:49
  • 4
    Possible duplicate of [Is an HTTP PUT request required to include a body?](http://stackoverflow.com/questions/1233372/is-an-http-put-request-required-to-include-a-body) – nikjohn Sep 02 '16 at 09:52

1 Answers1

0

if I were designing a client that really wanted to send an empty body, I'd pass Content-Length: 0. Indeed, depending on one's reading of the POST and PUT method definitions (RFC 2616 9.5, 9.6) one might argue that the body is implied to be required - but a reasonable way to handle no body would be to assume a zero-length body.

Semantically though, PUT requests should have bodies, even if they are empty. Excerpt from here

Community
  • 1
  • 1
nikjohn
  • 20,026
  • 14
  • 50
  • 86