List of usage examples for org.apache.http.entity ContentType MULTIPART_FORM_DATA
ContentType MULTIPART_FORM_DATA
To view the source code for org.apache.http.entity ContentType MULTIPART_FORM_DATA.
Click Source Link
From source file:com.sugarcrm.candybean.webservices.WSSystemTest.java
License:asdf
@Test public void testPutRequestsMultiPartFormData() { Map<String, Object> data = new HashMap<>(); data.put("key1", "value1"); data.put("key2", "value2"); data.put("key3", "value3"); try {// w ww . j ava 2s . com response = WS.request(WS.OP.PUT, uri + "/put", headers, data, ContentType.MULTIPART_FORM_DATA); } catch (Exception e) { Assert.fail(e.toString()); } Assert.assertTrue(response != null); Assert.assertEquals("value1", ((Map) response.get("form")).get("key1")); Assert.assertEquals("value2", ((Map) response.get("form")).get("key2")); Assert.assertEquals("value3", ((Map) response.get("form")).get("key3")); }
From source file:org.duracloud.common.web.RestHttpHelper.java
public HttpResponse multipartFilePost(String url, File file) throws Exception { ContentType contentType = ContentType.MULTIPART_FORM_DATA; HttpEntity reqEntity = MultipartEntityBuilder.create() .addBinaryBody(file.getName(), file, contentType, file.getName()).build(); return multipartPost(url, reqEntity); }
From source file:org.wso2.carbon.appmgt.sampledeployer.http.HttpHandler.java
public String doPostMultiData(String url, String method, MobileApplicationBean mobileApplicationBean, String cookie) throws IOException { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); httpPost.addHeader("Cookie", "JSESSIONID=" + cookie); MultipartEntityBuilder reqEntity;//from ww w. j a v a 2s.c o m if (method.equals("upload")) { reqEntity = MultipartEntityBuilder.create(); FileBody fileBody = new FileBody(new File(mobileApplicationBean.getApkFile())); reqEntity.addPart("file", fileBody); } else { reqEntity = MultipartEntityBuilder.create(); reqEntity.addPart("version", new StringBody(mobileApplicationBean.getVersion(), ContentType.MULTIPART_FORM_DATA)); reqEntity.addPart("provider", new StringBody(mobileApplicationBean.getMarkettype(), ContentType.MULTIPART_FORM_DATA)); reqEntity.addPart("markettype", new StringBody(mobileApplicationBean.getMarkettype(), ContentType.MULTIPART_FORM_DATA)); reqEntity.addPart("platform", new StringBody(mobileApplicationBean.getPlatform(), ContentType.MULTIPART_FORM_DATA)); reqEntity.addPart("name", new StringBody(mobileApplicationBean.getName(), ContentType.MULTIPART_FORM_DATA)); reqEntity.addPart("description", new StringBody(mobileApplicationBean.getDescription(), ContentType.MULTIPART_FORM_DATA)); FileBody bannerImageFile = new FileBody(new File(mobileApplicationBean.getBannerFilePath())); reqEntity.addPart("bannerFile", bannerImageFile); FileBody iconImageFile = new FileBody(new File(mobileApplicationBean.getIconFile())); reqEntity.addPart("iconFile", iconImageFile); FileBody screenShot1 = new FileBody(new File(mobileApplicationBean.getScreenShot1File())); reqEntity.addPart("screenshot1File", screenShot1); FileBody screenShot2 = new FileBody(new File(mobileApplicationBean.getScreenShot2File())); reqEntity.addPart("screenshot2File", screenShot2); FileBody screenShot3 = new FileBody(new File(mobileApplicationBean.getScreenShot3File())); reqEntity.addPart("screenshot3File", screenShot3); reqEntity.addPart("addNewAssetButton", new StringBody("Submit", ContentType.MULTIPART_FORM_DATA)); reqEntity.addPart("mobileapp", new StringBody(mobileApplicationBean.getMobileapp(), ContentType.MULTIPART_FORM_DATA)); reqEntity.addPart("sso_ssoProvider", new StringBody(mobileApplicationBean.getSso_ssoProvider(), ContentType.MULTIPART_FORM_DATA)); reqEntity.addPart("appmeta", new StringBody(mobileApplicationBean.getAppmeta(), ContentType.MULTIPART_FORM_DATA)); } final HttpEntity entity = reqEntity.build(); httpPost.setEntity(entity); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = httpClient.execute(httpPost, responseHandler); if (!method.equals("upload")) { String id_part = responseBody.split(",")[2].split(":")[1]; return id_part.substring(2, (id_part.length() - 2)); } return responseBody; }
From source file:org.jboss.as.test.integration.management.http.HttpDeploymentUploadUnitTestCase.java
private HttpEntity createUploadEntity(final File archive) { return MultipartEntityBuilder.create().setContentType(ContentType.MULTIPART_FORM_DATA) .setBoundary(BOUNDARY_PARAM).addTextBody("test1", BOUNDARY_PARAM) .addTextBody("test2", BOUNDARY_PARAM) .addBinaryBody("file", archive, ContentType.APPLICATION_OCTET_STREAM, DEPLOYMENT_NAME).build(); }
From source file:org.duracloud.common.web.RestHttpHelper.java
public HttpResponse multipartFileStreamPost(String url, String fileName, InputStream stream) throws Exception { ContentType contentType = ContentType.MULTIPART_FORM_DATA; HttpEntity reqEntity = MultipartEntityBuilder.create() .addBinaryBody(fileName, stream, contentType, fileName).build(); return multipartPost(url, reqEntity); }
From source file:com.github.avarabyeu.restendpoint.http.HttpClientRestEndpoint.java
@Override public final <RS> Will<Response<RS>> post(String resource, MultiPartRequest request, Class<RS> clazz) throws RestEndpointIOException { HttpPost post = new HttpPost(spliceUrl(resource)); try {/*w ww . j a va 2s . com*/ MultipartEntityBuilder builder = MultipartEntityBuilder.create(); for (MultiPartRequest.MultiPartSerialized<?> serializedPart : request.getSerializedRQs()) { Serializer serializer = getSupportedSerializer(serializedPart); builder.addPart(serializedPart.getPartName(), new StringBody(new String(serializer.serialize(serializedPart.getRequest())), ContentType.parse(serializer.getMimeType()))); } for (MultiPartRequest.MultiPartBinary partBinaty : request.getBinaryRQs()) { builder.addPart(partBinaty.getPartName(), new ByteArrayBody(partBinaty.getData().read(), ContentType.parse(partBinaty.getContentType()), partBinaty.getFilename())); } /* Here is some dirty hack to avoid problem with MultipartEntity and asynchronous http client * Details can be found here: http://comments.gmane.org/gmane.comp.apache.httpclient.user/2426 * * The main idea is to replace MultipartEntity with NByteArrayEntity once first * doesn't support #getContent method * which is required for async client implementation. So, we are copying response * body as byte array to NByteArrayEntity to * leave it unmodified. * * Alse we need to add boundary value to content type header. Details are here: * http://en.wikipedia.org/wiki/Delimiter#Content_boundary * MultipartEntity generates correct header by yourself, but we need to put it * manually once we replaced entity type to NByteArrayEntity */ String boundary = "-------------" + UUID.randomUUID().toString(); builder.setBoundary(boundary); ByteArrayOutputStream baos = new ByteArrayOutputStream(); builder.build().writeTo(baos); post.setEntity(new NByteArrayEntity(baos.toByteArray(), ContentType.MULTIPART_FORM_DATA)); post.setHeader("Content-Type", "multipart/form-data;boundary=" + boundary); } catch (Exception e) { throw new RestEndpointIOException("Unable to build post multipart request", e); } return executeInternal(post, new ClassConverterCallback<RS>(serializers, clazz)); }
From source file:com.sugarcrm.candybean.webservices.WS.java
/** * Private helper method to abstract creating a POST/PUT request. * Side Effect: Adds the body to the request * * @param request A PUT or POST request * @param body Map of Key Value pairs * @param contentType The intended content type of the body *//* w w w.j a v a 2 s . co m*/ protected static void addBodyToRequest(HttpEntityEnclosingRequestBase request, Map<String, Object> body, ContentType contentType) { if (body != null) { if (contentType == ContentType.MULTIPART_FORM_DATA) { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); for (Map.Entry<String, Object> entry : body.entrySet()) { builder.addTextBody(entry.getKey(), (String) entry.getValue()); } request.setEntity(builder.build()); } else { JSONObject jsonBody = new JSONObject(body); StringEntity strBody = new StringEntity(jsonBody.toJSONString(), ContentType.APPLICATION_JSON); request.setEntity(strBody); } } }
From source file:com.clickntap.vimeo.Vimeo.java
private VimeoResponse apiRequest(String endpoint, String methodName, Map<String, String> params, File file) throws IOException { CloseableHttpClient client = HttpClientBuilder.create().build(); HttpRequestBase request = null;/*w ww .j a v a2 s . com*/ String url = null; if (endpoint.startsWith("http")) { url = endpoint; } else { url = new StringBuffer(VIMEO_SERVER).append(endpoint).toString(); } if (methodName.equals(HttpGet.METHOD_NAME)) { request = new HttpGet(url); } else if (methodName.equals(HttpPost.METHOD_NAME)) { request = new HttpPost(url); } else if (methodName.equals(HttpPut.METHOD_NAME)) { request = new HttpPut(url); } else if (methodName.equals(HttpDelete.METHOD_NAME)) { request = new HttpDelete(url); } else if (methodName.equals(HttpPatch.METHOD_NAME)) { request = new HttpPatch(url); } request.addHeader("Accept", "application/vnd.vimeo.*+json; version=3.2"); request.addHeader("Authorization", new StringBuffer(tokenType).append(" ").append(token).toString()); HttpEntity entity = null; if (params != null) { ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); for (String key : params.keySet()) { postParameters.add(new BasicNameValuePair(key, params.get(key))); } entity = new UrlEncodedFormEntity(postParameters); } else if (file != null) { entity = new FileEntity(file, ContentType.MULTIPART_FORM_DATA); } if (entity != null) { if (request instanceof HttpPost) { ((HttpPost) request).setEntity(entity); } else if (request instanceof HttpPatch) { ((HttpPatch) request).setEntity(entity); } else if (request instanceof HttpPut) { ((HttpPut) request).setEntity(entity); } } CloseableHttpResponse response = client.execute(request); String responseAsString = null; int statusCode = response.getStatusLine().getStatusCode(); if (methodName.equals(HttpPut.METHOD_NAME) || methodName.equals(HttpDelete.METHOD_NAME)) { JSONObject out = new JSONObject(); for (Header header : response.getAllHeaders()) { out.put(header.getName(), header.getValue()); } responseAsString = out.toString(); } else if (statusCode != 204) { ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); responseAsString = out.toString("UTF-8"); out.close(); } JSONObject json = null; try { json = new JSONObject(responseAsString); } catch (Exception e) { json = new JSONObject(); } VimeoResponse vimeoResponse = new VimeoResponse(json, statusCode); response.close(); client.close(); return vimeoResponse; }
From source file:com.sugarcrm.candybean.webservices.WSSystemTest.java
License:asdf
@Test public void testPostRequestsMultiPartFormData() { Map<String, Object> data = new HashMap<>(); data.put("key1", "value1"); data.put("key2", "value2"); data.put("key3", "value3"); try {/*www. ja v a 2s . c o m*/ response = WS.request(WS.OP.POST, uri + "/post", headers, data, ContentType.MULTIPART_FORM_DATA); } catch (Exception e) { Assert.fail(e.toString()); } Assert.assertTrue(response != null); Assert.assertEquals("value1", ((Map) response.get("form")).get("key1")); Assert.assertEquals("value2", ((Map) response.get("form")).get("key2")); Assert.assertEquals("value3", ((Map) response.get("form")).get("key3")); }