12

I'm trying to make PUT request with JSON data using HttpURLConnection in Java. The way I do it doesn't work. I get no errors so I don't know what the problem is.

public static void main(String[] args) {

        URL url;
        try {
            url = new URL("http://fltspc.itu.dk/widget/515318fe17450f312b00153d/");
            HttpURLConnection hurl = (HttpURLConnection) url.openConnection();
            hurl.setRequestMethod("PUT");
            hurl.setDoOutput(true);
            hurl.setRequestProperty("Content-Type", "application/json");
            hurl.setRequestProperty("Accept", "application/json");

            String payload = "{'pos':{'left':45,'top':45}}";

            OutputStreamWriter osw = new OutputStreamWriter(hurl.getOutputStream());
            osw.write(payload);
            osw.flush();
            osw.close();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

And here is the request I'm actually trying to make:

Screenshot from Chrome Developer Tools

I was already making GET requests to the resource within the same app and it worked fine. I would be very grateful for all tips on how can I debug that or how can I try to do it some other way. So far I tried only using OutputStream instead of OutputStreamWriter but it doesn't work neither.

2
  • I use PUT because I'm modifying some data in the widget. More specifically I want to change the position of the widget on the surface. When I do it manually (using web interface) the application uses PUT like on the screenshot that I enclosed. I tried changing it POST and it doesn't work. Commented Mar 28, 2013 at 9:59
  • 2
    @Hanno - Using @PUT is a RESTFul convention for updating a resource, it is in no way specific to transferring a file. See the RFC - w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.6 Commented Aug 7, 2013 at 18:47

1 Answer 1

33

The Sun (Oracle) implementation of HttpURLConnection caches the content of your post unless you tell it to be in streaming mode. The content will be sent if you start interaction with the response such as:

hurl.getResponseCode();

Also, according to RFC 4627 you can not use single quotes in your json (although some implementations seem to not care).

So, change your payload to:

String payload = "{\"pos\":{\"left\":45,\"top\":45}}";

This example works for me

public class HttpPut {
    public static void main(String[] args) throws Exception {
        Random random = new Random();
        URL url = new URL("http://fltspc.itu.dk/widget/515318fe17450f312b00153d/");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("PUT");
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept", "application/json");
        OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
        osw.write(String.format("{\"pos\":{\"left\":%1$d,\"top\":%2$d}}", random.nextInt(30), random.nextInt(20)));
        osw.flush();
        osw.close();
        System.err.println(connection.getResponseCode());
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

@Booyaches Thought your problem was sending the payload. Updated with information of RFC 4627 which does not accept single quotes.
Updated with sample code which (viewing the surface you used in the url) moves the big green note to a random location.
I noticed that there's no connection.setConnectTimeout() - not necessary for PUT ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.