/** * Set the InputStream of request body data to be sent to the server. * * @param input InputStream of request body data to be sent to the server * @return an {@link HttpConnection} for method chaining * @deprecated Use {@link #setRequestBody(InputStreamGenerator)} */ public HttpConnection setRequestBody(final InputStream input) { // -1 signals inputLength unknown return setRequestBody(input, -1); }
/** * Set an InputStreamGenerator for an InputStream of request body data to be sent to the server. * * @param input InputStream of request body data to be sent to the server * @return an {@link HttpConnection} for method chaining * @since 2.4.0 */ public HttpConnection setRequestBody(final InputStreamGenerator input) { // -1 signals inputLength unknown return setRequestBody(input, -1); }
/** * Sets a JSON String as a request entity. * * @param connnection The request of {@link HttpConnection} * @param body The JSON String to set. */ public static void setEntity(HttpConnection connnection, String body, String contentType) { connnection.requestProperties.put("Content-type", contentType); connnection.setRequestBody(body); }
/** * Set the byte array of request body data to be sent to the server. * * @param input byte array of request body data to be sent to the server * @return an {@link HttpConnection} for method chaining */ public HttpConnection setRequestBody(final byte[] input) { return setRequestBody(new ByteArrayInputStream(input), input.length); }
/** * Set the InputStream of request body data, of known length, to be sent to the server. * * @param input InputStream of request body data to be sent to the server * @param inputLength Length of request body data to be sent to the server, in bytes * @return an {@link HttpConnection} for method chaining * @deprecated Use {@link #setRequestBody(InputStreamGenerator, long)} */ public HttpConnection setRequestBody(final InputStream input, final long inputLength) { try { return setRequestBody(new InputStreamWrappingGenerator(input, inputLength), inputLength); } catch (IOException e) { logger.log(Level.SEVERE, "Error copying input stream for request body", e); throw new RuntimeException(e); } }
/** * <p>Performs a HTTP POST request with JSON request body.</p> * <p>The stream <b>must</b> be closed after use.</p> * * @return Input stream with response */ public InputStream post(URI uri, String json) { HttpConnection connection = Http.POST(uri, "application/json"); if (json != null && !json.isEmpty()) { connection.setRequestBody(json); } return executeToInputStream(connection); }
/** * Performs a HTTP PUT request, saves an attachment. * * @return {@link Response} */ Response put(URI uri, InputStream instream, String contentType) { HttpConnection connection = Http.PUT(uri, contentType); connection.setRequestBody(instream); return executeToResponse(connection); }
private ChangesResult changesRequestWithPost( String body, final Map<String, Object> options) { URI changesFeedUri = uriHelper.changesUri(options); HttpConnection connection = Http.POST(changesFeedUri, "application/json"); connection.setRequestBody(body); return executeToJsonObjectWithRetry(connection, ChangesResult.class); }
public void putAttachmentStream(String id, String rev, String attachmentName, String contentType, byte[] attachmentData) { Misc.checkNotNullOrEmpty(id, "id"); Misc.checkNotNullOrEmpty(rev, "rev"); Map<String, Object> queries = new HashMap<String, Object>(); queries.put("rev", rev); URI doc = this.uriHelper.attachmentUri(id, queries, attachmentName); HttpConnection connection = Http.PUT(doc, contentType); connection.setRequestBody(attachmentData); //TODO check result? executeToJsonObjectWithRetry(connection, Response.class); }
HttpConnection asRequest() { DatabaseURIHelper builder = getViewURIBuilder(); for (Map.Entry<String, Object> queryParameter : processParameters(Parameter.Type .QUERY_PARAMETER).entrySet()) { builder.query(queryParameter.getKey(), queryParameter.getValue()); } Map<String, Object> parameters = processParameters(Parameter.Type.BODY_PARAMETER); // if there are no parameters in the body, we do a GET, otherwise we do a POST if (parameters == null || parameters.isEmpty()) { return Http.GET(builder.build()); } JsonElement body = gson.toJsonTree(parameters); HttpConnection conn = Http.POST(builder.build(), "application/json"); conn.setRequestBody(body.toString()); return conn; }
private List<Response> bulkCreateDocs(String payload) { URI uri = this.uriHelper.bulkDocsUri(); HttpConnection connection = Http.POST(uri, "application/json"); connection.setRequestBody(payload); return executeToJsonObjectWithRetry(connection, new CouchClientTypeReference<List<Response>>()); }
public Response putMultipart(final MultipartAttachmentWriter mpw) { Map<String, Object> options = new HashMap<String, Object>(); options.put("new_edits", "false"); URI uri = this.uriHelper.documentUri(mpw.getId(), options); String contentType = "multipart/related;boundary=" + mpw.getBoundary(); HttpConnection connection = Http.PUT(uri, contentType); connection.setRequestBody(mpw.makeInputStreamGenerator(), mpw.getContentLength()); return executeToJsonObjectWithRetry(connection, Response.class); }
public Response putUpdate(String id, Object document) { Misc.checkNotNullOrEmpty(id, "id"); Misc.checkNotNull(document, "Document"); URI doc = this.uriHelper.documentUri(id); String json = JSONUtils.toJson(document); HttpConnection connection = Http.PUT(doc, "application/json"); connection.setRequestBody(json); Response r = executeToJsonObjectWithRetry(connection, Response.class); return r; }
public static int executeHttpPostRequest(URI uri, String payload, CouchConfig config) { HttpConnection connection = Http.POST(uri, "application/json"); connection.requestInterceptors.addAll(config.getRequestInterceptors()); connection.responseInterceptors.addAll(config.getResponseInterceptors()); try { connection.setRequestBody(payload); connection.execute(); } catch (Exception e) { ; // ignore exception } try { return connection.getConnection().getResponseCode(); } catch (Exception e) { System.out.println("*** got ex "+e); return 0; } }
@TestTemplate public void inputStreamRetryBytes() throws Exception { HttpConnection request = Http.POST(mockWebServer.url("/").url(), "application/json"); byte[] content = "abcde".getBytes("UTF-8"); request.setRequestBody(content); testInputStreamRetry(request, content); }
@TestTemplate public void inputStreamRetryString() throws Exception { HttpConnection request = Http.POST(mockWebServer.url("/").url(), "application/json"); String content = "abcde"; request.setRequestBody(content); testInputStreamRetry(request, content.getBytes("UTF-8")); }
@TestTemplate public void inputStreamRetryWithLength() throws Exception { HttpConnection request = Http.POST(mockWebServer.url("/").url(), "application/json"); final byte[] content = "abcde".getBytes("UTF-8"); // Mock up an input stream that doesn't support marking request.setRequestBody(new UnmarkableInputStream(content), content.length); testInputStreamRetry(request, content); }
@TestTemplate public void inputStreamRetry() throws Exception { HttpConnection request = Http.POST(mockWebServer.url("/").url(), "application/json"); final byte[] content = "abcde".getBytes("UTF-8"); // Mock up an input stream that doesn't support marking request.setRequestBody(new UnmarkableInputStream(content)); testInputStreamRetry(request, content); }
@TestTemplate public void inputStreamRetryGenerator() throws Exception { HttpConnection request = Http.POST(mockWebServer.url("/").url(), "application/json"); byte[] content = "abcde".getBytes("UTF-8"); request.setRequestBody(new TestInputStreamGenerator(content)); testInputStreamRetry(request, content); }
@TestTemplate public void inputStreamRetryGeneratorWithLength() throws Exception { HttpConnection request = Http.POST(mockWebServer.url("/").url(), "application/json"); byte[] content = "abcde".getBytes("UTF-8"); request.setRequestBody(new TestInputStreamGenerator(content), content.length); testInputStreamRetry(request, content); }