List of usage examples for com.squareup.okhttp RequestBody create
public static RequestBody create(final MediaType contentType, final File file)
From source file:org.hawkular.datamining.itest.AbstractITest.java
License:Apache License
protected Response put(String path, String tenant, Object payload) throws Throwable { String json = mapper.writeValueAsString(payload); Request request = new Request.Builder().put(RequestBody.create(MEDIA_TYPE_JSON, json)).url(baseURI + path) .addHeader(Constants.TENANT_HEADER_NAME, tenant).build(); return execute(request); }
From source file:org.hawkular.integration.test.AbstractTestBase.java
License:Apache License
protected Response post(String path, String payload) throws Throwable { Request request = newAuthRequest().url(baseURI + path).post(RequestBody.create(MEDIA_TYPE_JSON, payload)) .build();// ww w . j av a2 s .c o m return client.newCall(request).execute(); }
From source file:org.hawkular.integration.test.Scenario1ITest.java
License:Apache License
private void postMetricValue(String resourceId, String metricName, int value, int timeSkewMinutes) throws IOException { long now = System.currentTimeMillis(); String tmp = resourceId + "." + metricName; long time = now + (timeSkewMinutes * 60 * 1000); String path = "/hawkular/metrics/gauges/" + tmp + "/data"; String json = "[{timestamp: " + time + ", value: " + value + "}]"; Request request = newAuthRequest().url(baseURI + path).post(RequestBody.create(MEDIA_TYPE_JSON, json)) .build();/*from w w w . j a v a2 s. co m*/ Response response = client.newCall(request).execute(); Assert.assertEquals("Response msg: " + response.body().string(), 200, response.code()); }
From source file:org.hawkular.rx.httpclient.OkClient.java
License:Apache License
public RestResponse post(String authToken, String persona, String url, String json) throws IOException { RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder().url(url).addHeader("Authorization", authToken) .addHeader("Hawkular-Persona", persona).post(body).build(); // this.delegatingClient.interceptors().add(null); Response response = this.delegatingClient.newCall(request).execute(); // todo: async // Response response = this.delegatingClient.newCall(request).enqueue(new Callback() { // @Override public void onFailure(Request request, IOException e) { ////from w w w. j a va 2s.c o m // } // // @Override public void onResponse(Response response) throws IOException { // // } // }); return new RestResponse(response); }
From source file:org.hawkular.rx.httpclient.OkClient.java
License:Apache License
public RestResponse put(String authToken, String persona, String url, String json) throws IOException { RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder().url(url).addHeader("Authorization", authToken) .addHeader("Hawkular-Persona", persona).put(body).build(); Response response = this.delegatingClient.newCall(request).execute(); return new RestResponse(response); }
From source file:org.jboss.arquillian.ce.proxy.AbstractProxy.java
License:Open Source License
public <T> T post(String url, Class<T> returnType, Object requestObject) throws Exception { final OkHttpClient httpClient = getHttpClient(); Request.Builder builder = new Request.Builder(); builder.url(url);//from ww w . j a va 2 s. c o m if (requestObject != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { oos.writeObject(requestObject); oos.flush(); } catch (Exception e) { throw new RuntimeException("Error sending request Object, " + requestObject, e); } RequestBody body = RequestBody.create(MediaType.parse("application/octet-stream"), baos.toByteArray()); builder.post(body); } Request request = builder.build(); Response response = httpClient.newCall(request).execute(); int responseCode = response.code(); if (responseCode == HttpURLConnection.HTTP_OK) { Object o; try (ObjectInputStream ois = new ObjectInputStream(response.body().byteStream())) { o = ois.readObject(); } if (returnType.isInstance(o) == false) { throw new IllegalStateException( "Error reading results, expected a " + returnType.getName() + " but got " + o); } return returnType.cast(o); } else if (responseCode == HttpURLConnection.HTTP_NO_CONTENT) { return null; } else if (responseCode != HttpURLConnection.HTTP_NOT_FOUND) { throw new IllegalStateException( "Error launching test at " + url + ". Got " + responseCode + " (" + response.message() + ")"); } return null; // TODO }
From source file:org.kegbot.api.KegbotApiImpl.java
License:Open Source License
private static RequestBody formBody(Map<String, String> params) { return RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), getUrlParamsString(params).getBytes()); }
From source file:org.kegbot.api.KegbotApiImpl.java
License:Open Source License
/** Builds a multi-part form body. */ private static RequestBody formBody(Map<String, String> params, Map<String, File> files) throws KegbotApiException { final String boundary = getBoundary(); final byte[] boundaryBytes = boundary.getBytes(); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final byte[] outputBytes; try {/* www. ja v a 2 s . c om*/ // Form data. for (final Map.Entry<String, String> param : params.entrySet()) { bos.write(HYPHENS); bos.write(boundaryBytes); bos.write(CRLF); bos.write(String.format("Content-Disposition: form-data; name=\"%s\"", param.getKey()).getBytes()); bos.write(CRLF); bos.write(CRLF); bos.write(param.getValue().getBytes()); bos.write(CRLF); } // Files for (final Map.Entry<String, File> entry : files.entrySet()) { final String entityName = entry.getKey(); final File file = entry.getValue(); bos.write(HYPHENS); bos.write(boundaryBytes); bos.write(CRLF); bos.write(String.format("Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"", entityName, file.getName()).getBytes()); bos.write(CRLF); bos.write(CRLF); final FileInputStream fis = new FileInputStream(file); try { ByteStreams.copy(fis, bos); } finally { fis.close(); } bos.write(CRLF); } bos.write(HYPHENS); bos.write(boundaryBytes); bos.write(HYPHENS); bos.write(CRLF); bos.flush(); outputBytes = bos.toByteArray(); } catch (IOException e) { throw new KegbotApiException(e); } return RequestBody.create(MediaType.parse("multipart/form-data;boundary=" + boundary), outputBytes); }
From source file:org.openhab.binding.bosesoundtouch.handler.BoseSoundTouchHandler.java
License:Open Source License
private int sendRequestInWebSocket(String url) { int myId = socketRequestId++; String msg = "<msg><header " + attrDeviceId + " url=\"" + url + "\" method=\"GET\"><request requestID=\"" + myId + "\"><info type=\"new\"/></request></header></msg>"; try {/*from www . j ava 2s.c om*/ socket.sendMessage(RequestBody.create(WebSocket.TEXT, msg)); } catch (IOException e) { onFailure(e, null); return -1; } return myId; }
From source file:org.openhab.binding.bosesoundtouch.handler.BoseSoundTouchHandler.java
License:Open Source License
private int sendRequestInWebSocket(String url, String infoAddon, String postData) { int myId = socketRequestId++; String msg = "<msg><header " + attrDeviceId + " url=\"" + url + "\" method=\"POST\"><request requestID=\"" + myId + "\"><info " + (infoAddon == null ? "" : infoAddon) + " type=\"new\"/></request></header><body>" + postData + "</body></msg>"; try {/*from w w w .j a v a 2 s. com*/ socket.sendMessage(RequestBody.create(WebSocket.TEXT, msg)); } catch (IOException e) { onFailure(e, null); return -1; } return myId; }