Java MultiPart post - java

I am trying to create a multipart post to a URL with the following body:
Content-Disposition: form-data; name="json"
Content-Type: "application/json; charset=UTF-8"
{"input1":"data1","input2":"data2","input3":"data3"}
--APIMultipartPost
Content-Disposition: form-data; name="filePath"; filename="myFile.dat"
Content-Length: 381645
Content-Type: text/plain
Content-Transfer-Encoding: binary
<!-- SNIP -->
<!-- The OWL file was included here in plain text (without the SNIPs) -->
<!-- SNIP -->
I tried using MultipartEntityBuilder to create the multipart post, but probably I do something wrong with the parameters.
Can someone help me with the java code for this post?
Here is my code:
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost uploadFile = new HttpPost(URL);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("input1", "data1");
builder.addTextBody("input2", "data2");
builder.addTextBody("input3", "data3");
builder.addBinaryBody("file", new File("C:/myFile.dat"), ContentType.APPLICATION_OCTET_STREAM, "myFile.dat");
HttpEntity multipart = builder.build();
uploadFile.setEntity(multipart);
HttpResponse response = httpClient.execute(uploadFile);
and the error I get:
HttpResponseProxy{HTTP/1.1 422 Unprocessable Entity [Server: nginx/1.6.0, Date: Wed, 28 Jan 2015 19:29:42 GMT, Content-Type: application/json;charset=utf-8, Content-Length: 89, Connection: keep-alive, Status: 422 Unprocessable Entity, X-Rack-Cache: invalidate, pass, X-Content-Type-Options: nosniff] ResponseEntityProxy{[Content-Type: application/json;charset=utf-8,Content-Length: 89,Chunked: false]}}
Thank you!

The response 422 Unprocessable Entity says
The 422 (Unprocessable Entity) status code means the server
understands the content type of the request entity (hence a
415(Unsupported Media Type) status code is inappropriate), and the
syntax of the request entity is correct (thus a 400 (Bad Request)
status code is inappropriate) but was unable to process the contained
instructions. For example, this error condition may occur if an XML
request body contains well-formed (i.e., syntactically correct), but
semantically erroneous, XML instructions.
so as you can see the request you send doesn't fit with the specification of the controller/service that you POST to. If you use a tool like TCPMon you can actually intercept the request that you send and check how it looks like. When I do it with your code, it looks like
POST /url HTTP/1.1
Content-Length: 739
Content-Type: multipart/form-data; boundary=L2EqtNqIEXOWRCYYrYH8FSP1JAD65wz6c
Host: 127.0.0.1:8090
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.3.6 (java 1.5)
Accept-Encoding: gzip,deflate
--L2EqtNqIEXOWRCYYrYH8FSP1JAD65wz6c
Content-Disposition: form-data; name="input1"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
data1
--L2EqtNqIEXOWRCYYrYH8FSP1JAD65wz6c
Content-Disposition: form-data; name="input2"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
data2
--L2EqtNqIEXOWRCYYrYH8FSP1JAD65wz6c
Content-Disposition: form-data; name="input3"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
data3
--L2EqtNqIEXOWRCYYrYH8FSP1JAD65wz6c
Content-Disposition: form-data; name="file"; filename="myFile.dat"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
--L2EqtNqIEXOWRCYYrYH8FSP1JAD65wz6c--
which is not what you want. You only need a request with two parts, one is the json data and the other is the file.
So if you change your code as the following.
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost uploadFile = new HttpPost("http://localhost:8090/1");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
String jsonStr = "{\"input1\":\"data1\",\"input2\":\"data2\",\"input3\":\"data3\"}";
builder.addTextBody("json", jsonStr, ContentType.APPLICATION_JSON);
builder.addBinaryBody("file", new File("/path/to/file"),
ContentType.TEXT_PLAIN, "myFile.dat");
HttpEntity multipart = builder.build();
uploadFile.setEntity(multipart);
HttpResponse response = httpClient.execute(uploadFile);
You can provide a request like the following.
POST /url HTTP/1.1
Content-Length: 468
Content-Type: multipart/form-data; boundary=mN_bWsS4QQnlPJksvinB_WUpl2Qi6zVVElUEEBKh
Host: 127.0.0.1:8090
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.3.6 (java 1.5)
Accept-Encoding: gzip,deflate
--mN_bWsS4QQnlPJksvinB_WUpl2Qi6zVVElUEEBKh
Content-Disposition: form-data; name="json"
Content-Type: application/json; charset=UTF-8
Content-Transfer-Encoding: 8bit
{"input1":"data1","input2":"data2","input3":"data3"}
--mN_bWsS4QQnlPJksvinB_WUpl2Qi6zVVElUEEBKh
Content-Disposition: form-data; name="file"; filename="myFile.dat"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: binary
--mN_bWsS4QQnlPJksvinB_WUpl2Qi6zVVElUEEBKh--
which is what you need.

For multipart request, the content-type has to be multipart/form-data, then you specify content-type for each content. You might want to refer to w3.org - Introduction to forms for the HTTP header examples.

Related

Jersey Client to over come ROBOTS response

I'm using below code to get the response from the server. When i used POSTMAN , it gives a different clear response and when i used the java code using jersey client, it is giving robots response assuming my request to be a malicious. I'm using jersey 1.9 version.
Query: How to overcome this robot
ObjectMapper mapper = new ObjectMapper();Client CLIENT = Client.create();
CLIENT.addFilter(new LoggingFilter(System.out));
WebResource webResource = null;
webResource = CLIENT.resource(url);
ClientResponse response = null;
response = webResource
.header("Cache-Control", "no-cache")
.header("Cookies", false)
.accept(MediaType.APPLICATION_JSON)
.get(ClientResponse.class)
;
The below is the header info from POSTMAN. On Session id is set to true in POSTMAN.
Accept-Ranges →bytes
Age →1
Cache-Control →public, max-age=0, must-revalidate, no-transform
Content-Encoding →gzip
Content-Length →5533
Content-Security-Policy →default-src 'self' https://www.xxxx.xxxx https://*.cwp.xxxx.xxxx
Content-Type →application/json
Content-language →en-US
Pragma →no-cache
Server →ngccc
Set-Cookie →SECSESSID=2aprjner5fomn8vrk4sfhbn464; expires=Tue, 23-Jan-2018 18:35:09 GMT; Max-Age=3600; path=/; secure; HttpOnly
Set-Cookie →SECSESSID=2aprjner5fomn8vrk4sfhbn464; expires=Tue, 23-Jan-2018 18:35:09 GMT; Max-Age=3600; path=/; secure; httponly
Set-Cookie →SECSESSID_2=asdasdasdxxsdsdxx; expires=Tue, 23-Jan-2018 17:59:09 GMT; Max-Age=1440; path=/; secure; httponly
Set-Cookie →incap_ses_899_110069=+lYvxV259l1WSI2o+LzxVsA==; path=/; Domain=.www.xxxx.xxxx
Strict-Transport-Security →max-age=31536000
Vary →Accept-Encoding
Via →1.1 varnish-v4, HTTPS/1.1 localhost.localdomain
X-CDN →Incapsula
X-Content-Type-Options →nosniff
X-Frame-Options →SAMEORIGIN
X-UA-Compatible →IE=edge
X-Varnish →436830982
X-XSS-Protection →1; mode=block
Below is the response from code:
<html>
<head>
<META NAME="robots" CONTENT="noindex,nofollow">
<script src="/_Incapsula_Resource?SWJIYLWA=2977">
</script>
<script>
(function() {
var z="";var b="7472797B766172207868723B7661";for (var i=0;i<b.length;i+=2){z=z+parseInt(b.substring(i, i+2), 16)+",";}z = z.substring(0,z.length-1); eval(eval('String.fromCharCode('+z+')'));})();
</script></head>
<body>
<iframe style="display:none;visibility:hidden;"
</body></html>
below is the response from POSTMAN:
{
"query": {
"offset": 0,
"count": 20,
"total": 379
}
}
Got this resolved with by adding a SSL connections related code in it. Due to which it recognized and authenticated this request and responded back with the valid response.

Multipart with JSON in spring rest is not working

I am trying to create a method which accepts both multipart/form-data and application/json content type in same request. I am using Spring Rest for creating the same. But the call does not reach the method when I requested from Fiddler. Please help
Service method
#RequestMapping(value = "/", method = RequestMethod.POST, produces = "application/json",consumes = "multipart/form-data")
public ResponseEntity<JSONObject> addField( #RequestParam int customerId,#RequestParam int teId,
#RequestPart("file") List<MultipartFile> multipartFiles, #RequestParam("toast") String toast, MultipartHttpServletRequest request){
Fiddler
Header
Content-Type: multipart/form-data; boundary=HereGoes;
Body
Content-Type: application/json
---------------------------acebdf13572468
Content-Disposition: form-data; name="fieldNameHere"; filename="file1.pdf"
Content-Type: application/pdf
<#INCLUDE *C:\Users\User\Desktop\file1.pdf*#>
---------------------------acebdf13572468--
Add correct file name in request body of fiddler. In your method it is #RequestPart("file"). So use "file" in "name" field instead of "fieldNameHere" which is generated by fiddler. Your request body should look like below.
---------------------------acebdf13572468
Content-Disposition: form-data; name="file"; filename="file1.pdf"
Content-Type: application/pdf
<#INCLUDE *C:\Users\User\Desktop\file1.pdf*#>
---------------------------acebdf13572468--

http file upload response structure

i am trying to create an http file upload receiver in java
and i was looking at the post response of file upload in http
POST /upload?upload_progress_id=12344 HTTP/1.1
Host: localhost:3000
Content-Length: 1325
Origin: http://localhost:3000
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryePkpFF7tjBAqx29L
<other headers>
------WebKitFormBoundaryePkpFF7tjBAqx29L
Content-Disposition: form-data; name="MAX_FILE_SIZE"
100000
------WebKitFormBoundaryePkpFF7tjBAqx29L
Content-Disposition: form-data; name="uploadedfile"; filename="hello.o"
Content-Type: application/x-object
<file data>
------WebKitFormBoundaryePkpFF7tjBAqx29L--
if the boundary string will be a part of the file that is being uploaded (by coincidence) how can i detect it ? is that even possible?
Well, as section 4.1 of RFC 2388 states:
4.1 Boundary
As with other multipart types, a boundary is selected that does not occur in any of the data.
So I guess that your browser will probably be smart enough to choose an appropriate boundary.

How to nest MimeBodyParts in JavaMail (standard email messages)?

I'm trying to send an html email (with a text alternative) that includes an embedded/inline image using JavaMail.
I wanted to see what a properly formatted message looked like so I sent one using gmail with an inline image and below is the original source. It seems as if they are nesting body parts in the message. How can I replicate this EXACTLY using javamail. I found no way to put a Multipart inside of another Multipart.
MIME-Version: 1.0 Received: by 10.50.75.3 with HTTP; Thu, 23 Feb 2012
20:15:34 -0800 (PST) Date: Thu, 23 Feb 2012 20:15:34 -0800
Delivered-To: x#gmail.com Message-ID:
Subject: c From: Foo Bar To: Foo
Bar Content-Type: multipart/related;
boundary=e89a8f3b9b051e124104b9ae03fb
--e89a8f3b9b051e124104b9ae03fb Content-Type: multipart/alternative; boundary=e89a8f3b9b051e123604b9ae03fa
--e89a8f3b9b051e123604b9ae03fa Content-Type: text/plain; charset=ISO-8859-1
plain text content goes here
--e89a8f3b9b051e123604b9ae03fa Content-Type: text/html; charset=ISO-8859-1
--e89a8f3b9b051e123604b9ae03fa--
--e89a8f3b9b051e124104b9ae03fb Content-Type: image/png; name="logo.png" Content-Transfer-Encoding: base64 Content-ID:
X-Attachment-Id: ii_135ad92205fc1ace
iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAAh0lEQVR42u3ZsQnAIBBGYVtHShVI
7ViprASXcw8HMCckEGyTkB98xWsE4WvOwnMhBG/tVrGaSOU0+Q5MVhMtdWAVBlY3HCyW+7nlbhqB
TiGAAAECBAhwMqB0AN8ANuUkgQzJvEPy1WP75C5AgAABAgQ4GXATAK4DUP8LOAsD87WGiIJriGj5
AwiiiyDxmymtAAAAAElFTkSuQmCC
--e89a8f3b9b051e124104b9ae03fb--
UPDATE:
Using the standard multipart inline image methods (like those linked to by lechlukasz) produces the following email, not quite what I need:
MIME-Version: 1.0 Received: by 10.236.146.106 with SMTP id
q70mr1894063yhj.0.1330071158663; Fri, 24 Feb 2012 00:12:38 -0800
(PST) Reply-To: Foo Bar Message-ID:
<20cf303bfc80f02cfa04b9b152db#google.com> Date: Fri, 24 Feb 2012
08:12:38 +0000 Subject: please oh please4 From: Foo Bar
To: Foo Bar Content-Type:
multipart/alternative; boundary=20cf303bfc80f02ce704b9b152d6
--20cf303bfc80f02ce704b9b152d6 Content-Type: text/plain; charset=UTF-8; format=flowed; delsp=yes Content-Transfer-Encoding:
base64
77+9UE5HDQoaDQoAAAANCklIRFIAAAAoAAAAKAgGAAAA77+977+977+9bQAAAO+/vUlEQVR477+9
77+977+977+9Ce+/vSAgDQoQRmFbR0oVSO+/vVjvv73vv70E77+9cw8HMCckEGzvv73vv70ffO+/ vWsE77+9a++/ve+/vXMhBG/vv71W77+977+9SO+/vTTvv70OTFYTLXVgFQZWNxws77+977+977+9 77+9bhrvv71OIe+/vQABAgQIcDLvv710AO+/vQA277+9JO+/vQzvv73vv71D77+977+9Y++/ve+/
vS5A77+9AAECBDgZcBMA77+9A1Dvv70LOAsD77+977+977+977+977+9a++/vWjvv70DCO+/ve+/ vSDvv73vv70p77+9AAAAAElFTkTvv71CYO+/vQ0K
--20cf303bfc80f02ce704b9b152d6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
--20cf303bfc80f02ce704b9b152d6--
You put a Multipart in another Multipart by creating a MimeBodyPart, setting the content of that to the Multipart, and adding that body part to the first Multipart.
You mean something like that:
http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#IncludingImagesWithHTML
Note that you need also modify your html to specify cid:identifier as image src.

Java library for reading multipart/form-data http body containing multiple files

Basically, the following is what the format of data look like (it should follow HTTP standard format)
Content-Type: multipart/form-data; boundary=--3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f
--3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f
Content-Disposition: form-data; name="foo1.jpg"; filename="foo1.jpg"
Content-Length: 5578
Content-Type: image/jpeg
<image data 1 omitted>
--3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f
Content-Disposition: form-data; name="foo2.jpg"; filename="foo2.jpg"
Content-Length: 327
Content-Type: image/jpeg
<image data 2 omitted>
--3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f--
You can take a look at this question which recommends using Apache Commons File upload

Categories