public static String post(String location, String jsonArgs) { Content result = null; try { StringEntity jsonEntity = new StringEntity(jsonArgs); result = Request.Post(location) .body(jsonEntity) .execute() .returnContent(); } catch (IOException e) { e.printStackTrace(); } return (result == null) ? null : result.toString(); }
public static String get(String location) { Content result = null; try { result = Request.Get(location) .execute() .returnContent(); } catch (IOException e) { e.printStackTrace(); } System.out.println("Test Utility result = " + result.toString()); return (result == null) ? null : result.toString(); }
@Override public Collection<PagerdutyIncident> loadUnresolvedIncidents() { Collection<PagerdutyIncident> pagerdutyIncidents = Lists.newArrayList(); try { URI uri = new URIBuilder().setScheme("https").setHost(pagerDutyHost).setPath("/api/v1/incidents") .setParameter("status", "triggered,acknowledged").build(); Content returnContent = Request.Get(uri).addHeader("Content-type", "application/json") .addHeader("Authorization", pagerDutyToken).execute().returnContent(); String response = returnContent.asString(); pagerdutyIncidents = extractIncidents(response); } catch (Exception e) { logger.error(e.getMessage(), e); } return pagerdutyIncidents; }
@Test public void test() throws IOException { final String sparkHelloWorldServiceUrl = this.getSparkHelloWorldServiceUrl(); final Content content = Request.Get(sparkHelloWorldServiceUrl + "/hello") .execute() .returnContent(); Assert.assertEquals("Hello World", content.asString()); }
public Token authenticate() throws IOException { final Gson gson = new GsonBuilder().create(); final Content response = Request.Post(this.metabaseHost.toString() + "/session") .setHeader("Content-Type", "application/json") .body(new StringEntity(gson.toJson(this.credential))) .execute().returnContent(); this.token = gson.fromJson(response.asString(), Token.class); this.authenticated = true; return this.token; }
public void setResponse(CloseableHttpResponse response) throws UnsupportedOperationException, IOException { this.response = response; if(response.getEntity()!=null) { byte[] byteArray = IOUtils.toByteArray(response.getEntity().getContent()); ContentType contentType = ContentType.get(response.getEntity()); response.close(); this.content=new Content(byteArray, contentType); } }
public Content getContent() throws IOException { // byte[] byteArray = IOUtils.toByteArray(response.getEntity().getContent()); // ContentType contentType = ContentType.get(response.getEntity()); // response.close(); // return new Content(byteArray, contentType); return content; }
@Test public void handleGetRequest() throws Exception { final Content content = Request.Get("http://localhost:8080/") .execute() .returnContent(); assertEquals("Hello World!", content.asString()); }
@Test public void handlePostRequest() throws Exception { final Content content = Request.Post("http://localhost:8080/Vader") .execute() .returnContent(); assertEquals("Hello Vader!", content.asString()); }
@Test public void handleStatic() throws Exception { final Content content = Request.Get("http://localhost:8080/static/test.txt") .execute() .returnContent(); assertEquals("Content", content.asString()); }
@Test public void handleException() throws Exception { final Content content = Request.Get("http://localhost:8080/exception") .execute() .returnContent(); assertEquals("Programmatic Error", content.asString()); }
/** * Fetch the body for a given HistoryRecord and add it to the record * * @param record HistoryRecord containing a URL to fetch * @return */ protected static HistoryRecord fetchBody(HistoryRecord record) { try { Content content = Request.Get(encodeSpecialChars(record.getUrl())) .execute() .returnContent(); if (content != null) { record.setBody(content.asString(Charsets.UTF_8)); } } catch (IOException e) { System.err.println("Failed to fetch " + record.getUrl() + ": " + e.getLocalizedMessage()); } return record; }
public static String post(String location) { Content result = null; try { result = Request.Post(location) .execute() .returnContent(); } catch (IOException e) { e.printStackTrace(); } return (result == null) ? null : result.toString(); }
public static void send() throws URISyntaxException { URIBuilder builder = new URIBuilder(_nodePollerURL); JSONObject receiveJSON = JSONFactoryUtil.createJSONObject(); int i = 0; for(RequestParams params: _requestParams){ JSONObject object = JSONFactoryUtil.createJSONObject(); object.put("portletId", params.getPortletId()).put("data", params.getData().toString()) .put("userIds", String.valueOf(params.getUserId())); // builder.addParameter("portletId", params.getPortletId()).addParameter("data", params.getData().toString()). // addParameter("userIds", String.valueOf(params.getUserId())); receiveJSON.put(i+"", object); i++; } _requestParams.clear(); builder.addParameter("receive", receiveJSON.toString()); final Request request = Request.Get(builder.build()); Future<Content> future = async.execute(request, new FutureCallback<Content>() { public void failed (final Exception e) { _log.error(e.getMessage() +": "+ request); PollerProcessorUtil.destroy(); } public void completed (final Content content) { _log.info("Request completed: "+ request); // _log.info("Response: "+ content.asString()); } public void cancelled () {} }); }
private String doHttpCall(int port) throws IOException { Content content = Request.Get(baseUrl + ":" + port + httpPath) .execute().returnContent(); return content.asString(); }
public List<Card> getPublicCards() throws IOException { final Content content = executeGetAuthenticatedMethod("/card/public"); final Gson gson = new GsonBuilder().create(); return gson.fromJson(content.asString(), new TypeToken<List<Card>>() { }.getType()); }
private Content executeGetAuthenticatedMethod(final String apiEndpoint) throws IOException { return Request.Get(this.metabaseHost.toString() + apiEndpoint) .setHeader("Content-Type", "application/json") .setHeader(HEADER_X_KEY, this.token.getId()) .execute().returnContent(); }
static String getPage(String targetURL) throws Exception { Request x = Request.Get(targetURL); Response y = x.execute(); Content z = y.returnContent(); return z.toString(); }