1

I'm writing my first web service using Jersey and tomcat. I would like to use the PUT method to upload a file from a web page using javascript. What I'm noticing is that my server is not getting the PUT request, while it seems to be getting GET and POST. This is my method on the server side:

@PUT
@Path("myPut")
@Produces(MediaType.TEXT_PLAIN)
public String myPut() {
   System.out.print("OK!");
   return "OK!";
}

and this is the code I wrote on the client side:

function myFunction() {
   var client = new XMLHttpRequest();
   client.open("PUT", "http://localhost:8080/.../myPut", false);
   client.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
   client.send("test");
}

If I simply replace the method with GET or POST (both on client and server), I get my "OK!" string in response (and printed). I also tried with curl and the server seems to answer as I expect:

curl -X PUT -v http://localhost:8080/.../myPut
* Adding handle: conn: 0xc2eee0
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0xc2eee0) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 8080 (#0)
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> PUT /.../myPut HTTP/1.1
> User-Agent: curl/7.32.0
> Host: localhost:8080
> Accept: */*
> 
< HTTP/1.1 200 OK
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Content-Type: text/plain
< Content-Length: 3
< Date: Sat, 16 Nov 2013 17:45:36 GMT
< 
* Connection #0 to host localhost left intact
OK!

I read a little bit and it seems PUT should work with both Firefox and Chrome, which means it is me doing something wrong (here for example, PUT is explicitly in the list; and also from the first answer here I guessed PUT should be available). Anyone able to correct?

If this way is totally wrong/unacceptable, could you suggest another technology to do this (uploading a local file, possibly allowing me to show a progress bar)?

Community
  • 1
  • 1
Luca Carlon
  • 9,546
  • 13
  • 59
  • 91
  • AFAIK, browsers usually don't support all REST methods. `DELETE` is another example that is likely not supported. Did you see some docs that state otherwise? – Blue Skies Nov 16 '13 at 17:58
  • But... https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest seems to suggest it should work in the definition of the open(...) method. – Luca Carlon Nov 16 '13 at 18:03
  • @BlueSkies all browsers support all methods when sending xhr requests. – Ray Nicholus Nov 16 '13 at 19:17
  • My web page is a local html file that is opened in the browser. It is not provided by the server. Can this be related to same-source policy? – Luca Carlon Nov 17 '13 at 11:50

2 Answers2

0

Issue was caused by same-origin policy. Returning the page from the web service itself solved the issue. Otherwise CORS would be a possible solution.

Luca Carlon
  • 9,546
  • 13
  • 59
  • 91
-1

Tomcat by default does not supported the PUT method. You need to configure it. See here http://www.codereye.com/2010/12/configure-tomcat-to-accept-http-put.html?m=1

And you can test all verbs using chrome extension named "Advanced Rest client"

KayakDave
  • 24,636
  • 3
  • 65
  • 68