Re: Need help getting PUT request to work - web api 1.6.3

63 views
Skip to first unread message

apaladino

unread,
Oct 9, 2012, 12:36:53 PM10/9/12
to reviewb...@googlegroups.com
I think what might be happening here is that the parameters aren't being received by the API service, either on the server side or during transport. 

On Monday, October 8, 2012 8:58:21 AM UTC-7, Andy Paladino wrote:
Hi. I am trying to update the target groups for a review request. I got it to work with curl, but getting it to work via Java code is a not working. I've tried using rest and also with HttpClient.  when I try with Rest I get a 500 response code.  When I try with HttpClient, I get a 200 response code, but nothing gets updated.  Can someone tell me what i am doing wrong? thanks


****** REST - gets a 500 Response code  ****
    public void testPut_rest() throws Exception{
        String reviewId= "4969";
        String authToken = "Basic YXBhbGFkaW5vOkNpdHJpeDExBYc=";
        String url = "https://reviewboard-ouraddress-it.net/api/review-requests/"+reviewId+"/draft/";

        //curl -k -H "Authorization: Basic YXBhbGFkaW5vOkNpdHJpeDExBYc=" -X PUT "https://reviewboard-ouraddress-it.net/api/review-requests/4968/draft/" -d"api_format=json&expand=target_groups&target_groups=dev-adminservice%2C%20"


        try {

            SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory() {
                @Override
                protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
                    super.prepareConnection(connection, httpMethod);
                    connection.setInstanceFollowRedirects(true);
                }

            };
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
            restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
            restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
            restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
            restTemplate.setRequestFactory(requestFactory);
            HttpHeaders requestHeaders = new HttpHeaders();
            requestHeaders.set("Authorization", authToken);

            HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);

            Map<String, String> args = new HashMap<String, String>();
            args.put("api_format", "json");
            args.put("expand", "target_groups");
            args.put("target_groups", "dev-adminservice");


            HttpEntity<Map> response =
                    restTemplate.exchange(url, HttpMethod.PUT, requestEntity, Map.class, args);

            System.out.println(response.getBody());



        } catch (Exception e) {
            e.printStackTrace();
        }

    }

****** HTTPClient ******
    @Test
    public void testPut_HttpClient() throws Exception{
        String reviewId= "4969";
        String authToken = "Basic YXBhbGFkaW5vOkNpdHJpeDExBYc=";
        String url = "https://reviewboard-ouraddress-it.net/api/review-requests/"+reviewId+"/draft/";

        /*
            This works!
            curl -k -H "Authorization: Basic YXBhbGFkaW5vOkNpdHJpeDExBYc=" -X PUT "https://reviewboard-ouraddress-it.net/api/review-requests/4968/draft/" -d"api_format=json&expand=target_groups&target_groups=dev-adminservice%2C%20"
         */


        try {
            HttpClient client = new HttpClient();
            PutMethod method = new PutMethod(url);

            client.getParams().setParameter("api_format", "json");
            client.getParams().setParameter("expand", "target_groups");
            client.getParams().setParameter("target_groups", "dev-adminservice");

            method.addRequestHeader("Authorization", authToken);
            method.addRequestHeader("Content-Type", "application/json");

            int statusCode = client.executeMethod(method);

            if (statusCode != -1) {
                String response = method.getResponseBodyAsString();
                System.out.println(response);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

    }


*** 200 Repsonse code with following ouput
{"stat": "ok", "draft": {"last_updated": "2012-10-08 08:46:07", "description": "", "target_people": [], "changedescription": "", "target_groups": [], "links": {"self": {"href": "https://reviewboard-ouraddress-it.net/api/review-requests/4969/draft/", "method": "GET"}, "update": {"href": "https://reviewboard-ouraddress-it.net/api/review-requests/4969/draft/", "method": "PUT"}, "draft_screenshots": {"href": "https://reviewboard-ouraddress-it.net/api/review-requests/4969/draft/screenshots/", "method": "GET"}, "draft_file_attachments": {"href": "https://reviewboard-ouraddress-it.net/api/review-requests/4969/draft/file-attachments/", "method": "GET"}, "review_request": {"href": "https://reviewboard-ouraddress-it.net/api/review-requests/4969/", "method": "GET", "title": "(no summary)"}, "delete": {"href": "https://reviewboard-ouraddress-it.net/api/review-requests/4969/draft/", "method": "DELETE"}}, "bugs_closed": [], "public": false, "testing_done": "", "branch": "", "summary": "", "id": 4720}}




Christian Hammond

unread,
Oct 9, 2012, 4:45:44 PM10/9/12
to reviewb...@googlegroups.com, reviewb...@googlegroups.com
Can you trace what's being sent over the wire? I can't really give any useful advice without knowing where and how it's actually failing.

Christian

apaladino

unread,
Oct 10, 2012, 2:04:26 AM10/10/12
to reviewb...@googlegroups.com
Hi. I got it to work by doing a straight post to the draft url using apache commons PostMethod.  Still don't know why the put request isn't working for me, but at least i can continue on.  You can probably just copy my example code and change the reviewId to test this locally if you still wish to pursue this.

Andy P
Reply all
Reply to author
Forward
0 new messages