Upload file from MultipartEntityBuilder, added headers in file - apache-httpclient-4.x

I upload a JSON file using MultipartEntityBuilder. on another side when I read the file some headers are added on both top and bottom of the file. I tried many things to remove headers from a file but no success.
here is the code how I write multipart request
HttpPost request = new HttpPost("/getFile");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("file", new File("abc.json"), ContentType.APPLICATION_JSON, "abc.json");
HttpEntity entity = builder.build();
request.setEntity(entity);
uploaded file
--ZD1OkvwbsjLAuhqTLx2I6HzWkZcH9oHoBbTN Content-Disposition: form-data; name="upfile";
filename="abc.json"
Content-Type: application/octet-stream
{ "version": "1.2", }
--ZD1OkvwbsjLAuhqTLx2I6HzWkZcH9oHoBbTN Content-Disposition: form-data; name="text"
This is a multipart post
--ZD1OkvwbsjLAuhqTLx2I6HzWkZcH9oHoBbTN--
how can I send only JSON part not these top and bottom headers

Related

RestSharp: Naming the parameter using request.AddJsonBody()?

How can I name the piece of multipart/form-data given added to a request when I use the AddJsonBody() method?
I am attempting to get past the obsolete AddParameter() method.
Here's my code using the AddJsonBody() method:
request.AddJsonBody(Metadata);
There's an overload that allows me to specify the Content-Type, but I just need plain old application/json so I'm not using it. Here are the resulting relevant parts of my HTTP request as sent:
POST https://redacted/redacted HTTP/1.1
Content-Type: multipart/form-data; boundary=---------369C5A1F-30CF-450D-A5B4-2DBD93676056
-----------369C5A1F-30CF-450D-A5B4-2DBD93676056
Content-Type: application/json
Content-Disposition: form-data; name="application/json"
{"Date":"2021-07-28T14:27:01.0718841","FurtherInfo":"This is a metadata test."}
-----------369C5A1F-30CF-450D-A5B4-2DBD93676056
Content-Disposition: form-data; name="file"; filename="file20210728T1427010660244Z.txt"
Content-Type: text/plain
CL7~f`lz4ULMJa;]p-q!uH(-z*4iO'SHD)KYER5SI|e{3zW7^}J,%QPyD)$\K"
[...]
-----------369C5A1F-30CF-450D-A5B4-2DBD93676056--
As you can see, the "name" of the added parameter is application/json. I want it to be "metadata" instead. So I'm using this code to get things to be sent how I want, but this code is marked as obsolete:
Parameter metadata = new Parameter("metadata", JsonConvert.SerializeObject(Metadata), "application/json", ParameterType.RequestBody);
request.AddParameter(metadata);
Using this changes the HTTP request to:
POST https://redacted/redacted HTTP/1.1
Content-Type: multipart/form-data; boundary=---------8C24DE69-C111-418A-9C29-5D9DFABA320F
-----------8C24DE69-C111-418A-9C29-5D9DFABA320F
Content-Type: application/json
Content-Disposition: form-data; name="metadata"
{
"date": "2021-07-28T14:45:01.4650889",
"furtherInfo": "This is a metadata test."
}
-----------8C24DE69-C111-418A-9C29-5D9DFABA320F
Content-Disposition: form-data; name="file"; filename="file20210728T1445014611849Z.txt"
Content-Type: text/plain
zoRC)Z:c]\<#/z_q,k
[...]
-----------8C24DE69-C111-418A-9C29-5D9DFABA320F--
The specific serialization doesn't matter, only that it's valid JSON and has the name metadata.
Is there a way to use the newer AddJsonBody() method to do this? Is manipulating the parameter name on the roadmap?
as my understanding you are trying to send a file?
thi sis an example to send a file to API using RestSharp
using (WindowsIdentity.GetCurrent().Impersonate())
{
RestClient client = new RestClient(url);
var request = new RestRequest("/api/Upload", Method.POST, DataFormat.Json);
request.AddHeader("Cache-Control", "no-cache");
request.AlwaysMultipartFormData = true;
request.UseDefaultCredentials = true;
request.AddHeader("Content-Type", "multipart/form-data");
request.AddParameter("ServiceID", ServiceID, ParameterType.QueryString);
request.AddParameter("Description", description, ParameterType.QueryString);
//get byte[] , in reallife would be a stream
byte[] fileData = File.ReadAllBytes($#"C:\1\TEST\{filename}");
request.AddFileBytes("DocumentFile", fileData, filename, "multipart/form-data");
var result = client.Execute(request).Dump();
}
I found it!
From the class definition of RestRequest:
public IRestRequest AddParameter(string name, object value, string contentType, ParameterType type)
Instead of creating the Parameter, I can just add the parameter directly:
request.AddParameter("metadata", JsonConvert.SerializeObject(Metadata), "application/json", ParameterType.RequestBody);
And no more obsolete warning!

Postman - Unable to send request body as JSON file in Formdata

I am trying to create a PUT request in POSTMAN . I am passing request body formdata, where I browse and select a json file "test.json" which contains request in json format.
I have updated the Content-Type as application/json.
But it's throwing following error.
"Input string
'----------------------------919570725980811960289934' is not a valid
number. Path '', line 1, position 52."
The content of the Json File is as follows
{
"id":2,
"ipAddress":"0.0.0.0",
"port":50667,
"status":0,
"dataManagerSourceName":"aaab"
}
PFB the screenshots
Request with headers
Request Body form data section

Display image from s3

I am trying to display images that I have uploaded to S3. I believe the permissions are correct as I can hit the url and get data back. However when I put the URL in an image tag I get a broken link.
When I put the image tag on my site in the network tab I see the call to s3 (with a 200 response) but an empty body. If I hit the s3 url directly I get the data back and in the network tab I see the call WITH a response body from s3.
I'm uploading the images from a Vue frontend to an express/node backend. I see the images fine in s3.
s3.putObject({
Bucket: 'MYBUCKET',
Key: `${result._id}-${file.upload.filename}`,
Body: file.dataURL,
ACL: 'public-read',
ContentType: 'binary',
ContentEncoding: 'utf8'
}, function (resp) {});
Fiddle: https://jsfiddle.net/Xadvz/7327/
I use the data I get back from hitting s3 directly and it displays the image but when I put in the s3 url it displays a broken link.
S3 does not support dataURL data format.
In your backend:
convert dataURL to buffer
set ContentType of S3 object according to dataURL
code:
var regex = /^data:.+\/(.+);base64,(.*)$/;
var matches = file.dataURL.match(regex);
var ext = matches[1]; // image/jpeg
var data = matches[2]; // base64 string
var buffer = new Buffer(data, 'base64');
s3.putObject({
Bucket: 'MYBUCKET',
Key: `${result._id}-${file.upload.filename}`,
Body: buffer, // upload binary
ACL: 'public-read',
ContentType: ext // set content type
}, function (resp) {});
Are you sure Content-Type: 'binary' is correct? The value is identical to MIME type. Here's some typical types:
*.jpg .................................. image/jpeg
*.html ................................. text/html
*.txt ..................................... text/plain
*.mp3 .................................. audio/mpeg
Unknown/Default .............. application/octet-stream
Content-Type values must conform to the same syntax as MIME type:
type/sub-type
If you aren't sure you should at least use application/octet-stream

upload text files to server through html and then reading on server

I can already upload files and then read them on the server, I just have a problem with the extra data at the beginning of the input stream. My code in jsp:
<%
DataInputStream dis = new DataInputStream(request.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(dis));
String strLine;
while ((strLine = br.readLine()) != null) {
out.println (strLine+"");
}
%>
I get this data at the beginning that I would like to skip
-----------------------------41184676334 Content-Disposition: form-data; name="data"; filename="data.txt" Content-Type: text/plain
and also this at the end
-----------------------------41184676334--
I would to make an universal solution for this for any text file.

Can't set the content-type to Json in functional test

I have the following code to make a json request, but it fails:
Response response = POST("/b/profile/","application/json",body);
I also tried this one:
Response response = POST("/b/profile/","application/x-www-form-urlencoded",body);
but the response again had the content-type text/html. Any ideas?
Try
Response response = POST("/b/profile/","text/javascript",body);
See here for more details