private Response sendJsonPostOrPut ( OauthToken token , String url , String json , int connectTimeout , int readTimeout , String method ) throws IOException { LOG . debug ( "Sending JSON " + method + " to URL: " + url ) ; Response response = new Response ( ) ; HttpClient httpClient = createHttpClient ( connectTimeout , readTimeout ) ; HttpEntityEnclosingRequestBase action ; if ( "POST" . equals ( method ) ) { action = new HttpPost ( url ) ; } else if ( "PUT" . equals ( method ) ) { action = new HttpPut ( url ) ; } else { throw new IllegalArgumentException ( "Method must be either POST or PUT" ) ; } Long beginTime = System . currentTimeMillis ( ) ; action . setHeader ( "Authorization" , "Bearer" + " " + token . getAccessToken ( ) ) ; StringEntity requestBody = new StringEntity ( json , ContentType . APPLICATION_JSON ) ; action . setEntity ( requestBody ) ; try { HttpResponse httpResponse = httpClient . execute ( action ) ; String content = handleResponse ( httpResponse , action ) ; response . setContent ( content ) ; response . setResponseCode ( httpResponse . getStatusLine ( ) . getStatusCode ( ) ) ; Long endTime = System . currentTimeMillis ( ) ; LOG . debug ( "POST call took: " + ( endTime - beginTime ) + "ms" ) ; } finally { action . releaseConnection ( ) ; } return response ; }
PUT and POST are identical calls except for the header specifying the method