Attachment name in Outlook looks wrong (encoding issue?) - outlook

I hope someone can point us out to the issue in our code. Take a look at the picture.
As you can see the attachment name is ALMOST as expected. We want the name to be 'Anmälan-A00045.pdf' and 'Anmälan-45tg.pdf'. If I save the attachment the name is shown correctly. I believe that I am setting the encoding wrong somewhere. Trying to dump the mail as a file leads to the following output.
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="8EC249FE-78A0-420A-98D9-D953E58A8F9E"
--8EC249FE-78A0-420A-98D9-D953E58A8F9E
MIME-Version: 1.0
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: 7bit
Content-ID: {4EE462B2-16FB-47AE-A318-1C2C6ED889D3}
Content-Description: body
(body of email above and below is the attachment stuff)
--8EC249FE-78A0-420A-98D9-D953E58A8F9E
MIME-Version: 1.0
Content-Type: application/pdf
Content-Transfer-Encoding: base64
Content-ID: {F4F32EDB-36AF-4DAE-982A-93C3BF0F59C6}
Content-Description: Anmälan-45tg.pdf
Content-Disposition: attachment;
filename="=?utf-8?B?QW5tw6RsYW4tNDV0Zy5wZGZ=?="
We are using Biztalk with a custom send pipeline (containing our pipeline component and the mime/smime in encoding stage) to send out an email using SMTP-adapter. Now, as with many other posts in SO, this works perfect in other email clients like Gmail or Hotmail.
Now, code snippet below is what I am using to set the attachments and everything related to it. Does anyone have an idea of what to try? My trials with the method HtmlEncode did nothing, unfortunately.
public void ProcessAttachments(IPipelineContext _pContext, IBaseMessage _msg,
IList<Attachment> _attachments)
{
foreach (var item in _attachments)
{
var msgPart = _pContext.GetMessageFactory().CreateMessagePart();
msgPart.PartProperties.Write("FileName", "http://schemas.microsoft.com/BizTalk/2003/mime-properties", item.FileName + item.Extension);
msgPart.PartProperties.Write("ContentDescription", "http://schemas.microsoft.com/BizTalk/2003/mime-properties", item.ContentType);
// MIME / SMIME content transfer encoding
msgPart.PartProperties.Write("ContentTransferEncoding", "http://schemas.microsoft.com/BizTalk/2003/mime-properties", Encoding.UTF8);
msgPart.Data = new MemoryStream(BytesFromBase64String(item.Base64));
msgPart.ContentType = item.ContentType;
//msgPart.Charset = "UTF-8";
//_msg.AddPart(HtmlEncode(item.FileName + item.Extension), msgPart, false);
_msg.AddPart(item.FileName + item.Extension, msgPart, false);
}
}
private static string HtmlEncode(string filename)
{
/* Old code. What this does is change 'ä' to 'ä' Matters little as Outlook intreprets it literally. The filename in Outlook is set as 'Anmälan-45tg.pdf'.
string result;
using (StringWriter sw = new StringWriter())
{
var x = new HtmlTextWriter(sw);
x.WriteEncodedText(filename);
result = sw.ToString();
}
return result;
*/
var utf8 = Encoding.UTF8;
byte[] utfBytes = utf8.GetBytes(filename);
return utf8.GetString(utfBytes);
}

Related

Accessing multipart form data inside Wicket Ajax Behavior

I'm currently using Wicket and a jQuery plugin to crop picture ("croppic") and it need to request with ajax my back-end to crop the picture. The data are sent in a multipart format.
My Wicket back-end is an Ajax behavior with the "onRequest" method and I don't know how to retrieve the multipart data.
#Override
public void onRequest() {
String json = "{}";
boolean hasError = false;
RequestCycle cycle = getComponent().getRequestCycle();
IRequestParameters parameters = cycle.getRequest().getPostParameters();
This code only access to classic POST variables but cannot used to multipart form data (values are empty).
Do you know how to proceed for that?
PS: this thread is helpful but not understandable for me : Wicket 6 - Capturing HttpServletRequest parameters in Multipart form?
The body payload:
------WebKitFormBoundarykpVsQAYFGJywlAZd
Content-Disposition: form-data; name="imgUrl"
https://scontent.xx.fbcdn.net/hprofile-xpf1/t31.0- 1/c0.0.1536.1536/13055008_225242101175595_5770204993752392511_o.jpg
------WebKitFormBoundarykpVsQAYFGJywlAZd
Content-Disposition: form-data; name="imgInitW"
1536
------WebKitFormBoundarykpVsQAYFGJywlAZd
Content-Disposition: form-data; name="imgInitH"
1536
------WebKitFormBoundarykpVsQAYFGJywlAZd
Content-Disposition: form-data; name="imgW"
500
------WebKitFormBoundarykpVsQAYFGJywlAZd
Content-Disposition: form-data; name="imgH"
500
------WebKitFormBoundarykpVsQAYFGJywlAZd
Content-Disposition: form-data; name="imgY1"
etc...
Try with:
WebRequest webRequest = (WebRequest) cycle.getRequest();
MultipartServletWebRequest multiPartRequest = webRequest.newMultipartWebRequest(getMaxSize(), "ignored");
multiPartRequest.parseFileParts();
IRequestParameters params = multiPartRequest.getRequestParameters();
Here is my final code that works... very ugly but it works properly.
#Override
public void onRequest() {
boolean hasError = false;
IRequestParameters parameters = null;
RequestCycle cycle = RequestCycle.get();
ServletWebRequest webRequest = (ServletWebRequest) cycle.getRequest();
try {
MultipartServletWebRequest multiPartRequest = webRequest.newMultipartWebRequest(Bytes.kilobytes(10), "ignored");
multiPartRequest.parseFileParts();
parameters = multiPartRequest.getRequestParameters();
} catch (FileUploadException e) {
hasError = true;
}
After that you can easily call :
parameters.getParameterValue("you_param");

Intellij IDEA REST Client file uploading

I am trying to upload file through Intellij IDEA REST Client. I choose "File to upload (multipart/form-data)" and choose file to send. What is parameter name of this file? In my Sprint Controller i use this code
#RequestMapping(method = RequestMethod.POST, value = "/{id}/photo")
public void setCover(#PathVariable int id,
#RequestParam MultipartFile file) {
System.out.println(file.getName());
}
I also tried different names, such as "file", "fileToSend", "File to send" for #RequestParam, but Spring always cant find MultipartFile paramater.
I use the following code which works for me:
POST http://localhost:9003/goods/add HTTP/1.1
Content-Type: multipart/form-data; boundary=boundary
--boundary
Content-Disposition: form-data; name="file"; filename="file.csv"
// The 'input.txt' file will be uploaded
< /Users/xing/Desktop/file.csv
--boundary
Content-Disposition: form-data; name="advertType"
1
--boundary
// The method in the Spring controller
public Xxxx add(#RequestParam("file") MultipartFile file, Long advertType) {
For more information, please refer to https://www.jetbrains.com/help/ruby/exploring-http-syntax.html#use-multipart-form-data
File upload is not allowed due to security concern, not for application running on local machine. This solution worked for me. Its based on the comment by vincent.
See below:
POST http://localhost:8080/api/test HTTP/1.1
Content-Type: multipart/form-data; boundary=WebAppBoundary
--WebAppBoundary
Content-Disposition: form-data; name="param1"; filename="myfile.csv"
Content-Type: application/csv
// below will the path to the file (i.e. myfile.csv)
< C:/users/user/data/sample/myfile.csv
--WebAppBoundary
Content-Disposition: form-data; name="param2"
// value of param2
test
--WebAppBoundary
###
If you wish to do multipart file uploads inside IntelliJ IDEA's REST Client, please upvote this bug report => https://youtrack.jetbrains.com/issue/WEB-20197

How do you send a RESTLET POST request without the optional parameter "; CHARSET=UTF-8" in the HTTP header?

How do you send a RESTLET POST request without the optional parameter "; CHARSET=UTF-8" in the HTTP header?
When I use WGET in UNIX to try this proof of concept for my service, I can specify the optional Content-type: ... in the HTTP header like this:
Specifying the encoding as UTF-8:
wget --header="Content-Type: application/x-www-form-urlencoded; charset=UTF-8" --post-file=input_file.xml https://localhost/myservice
Omitting the encoding:
wget --header="Content-Type: application/x-www-form-urlencoded" --post-file=input_file.xml https://localhost/myservice
When I do something like this in RESTLET
Form form = new Form();
String accessToken = "Moroni123";
form.set("access_token", accessToken);
If I do this to see the representation:
Representation rep = form.getWebRepresentation();
I see this using the rep.toString() method:
[application/x-www-form-urlencoded,UTF-8]
Is there a way I can get it to look like this? SoapUI sends it without "UTF-8", how can I do this in RESTLET?
[application/x-www-form-urlencoded]
In fact, this hint corresponds to the character set within the Restlet representation. You can simply set it to null with the method setCharacterSet on the representation and you won't have anymore the UTF-8 in the header Content-Type. The following code describes this:
Form form = new Form();
String accessToken = "Moroni123";
form.set("access_token", accessToken);
Representation repr = form.getWebRepresentation();
MediaType mediaType = repr.getMediaType();
// corresponds to application/x-www-form-urlencoded
CharacterSet characterSet = repr.getCharacterSet();
// corresponds to UTF-8
rep.setCharacterSet(null);
ClientResource cr = new ClientResource("http://...");
cr.post(repr);
We will have the following request:
POST / HTTP/1.1
Date: Thu, 19 Feb 2015 15:58:06 GMT
Content-Type: application/x-www-form-urlencoded
Accept: */*
User-Agent: Restlet-Framework/2.3.0
Cache-Control: no-cache
Pragma: no-cache
Host: 127.0.0.1:8181
Connection: keep-alive
Content-Length: 22
access_token=Moroni123
Without this, the content of the header Content-type is:
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Hope it helps,
Thierry

Facebook graph api: upload multipart/form-data encoded image

I am trying to post an image using the source parameter (Multipart/form-data) in the Graph API explorer. My source parameter looks like this:
{Content-Type: multipart/form-data; boundary=xxxsrixxx
--xxxsrixxx
Content-Disposition: attachment; filename=try2.gif
Content-Type: image/gif
Content-Transfer-Encoding: binary,base64
R0lGODlhXgExAYeAAQAAAAEBAQICAgMDAwQEBAUFBQYGBgcHBwgICAkJCQoKCgsLCwwMDA0NDQ4ODg8PDxAQEBERERISEhMTExQUFBUVFRYWFhcXFxgYGBkZGRoaGhsbGxwcHB0dHR4eHh8fHyAgICEhISIiIiMjIyQkJCUlJSYmJicnJygoKCkpKSoqKisrKywsLC0tLS4uLi8vLzAwMDExMTIyMjMzMzQ0NDU1NTY2Njc3Nzg4ODk5OTo6Ojs7Ozw8PD09PT4+Pj8/P0BAQEFBQUJCQkNDQ0REREVFRUZGRkdHR0hISElJSUpKSktLS0xMTE1NTU5OTk9PT1BQUFFRUVJSUlNTU1RUVFVVVVZWVldXV1hYWFlZWVpaWltbW1xcXF1dXV5eXl9fX2BgYGFhYWJiYmNjY2RkZGVlZWZmZmdnZ2hoaGlpaWpqamtra2xsbG1tbW5ubm9vb3BwcHFxcXJycnNzc3R0dHV1dXZ2dnd3d3h4eHl5eXp6ent7e3x8fH19fX5+fn9/fzL/oICAgIGBgYKCgoODg4SEhIWFhYaGhoeHh4iIiImJiYqKiouLi4yMjI2NjY6Ojo+Pj5CQkJGRkZKSkpOTk5SUlJWVlZaWlpeXl5iYmJmZmZqampubm5ycnJ2dnZ6enp+fn6CgoKGhoaKioqOjo6SkpKWlpaampqenp6ioqKmpqaqqqqurq6ysrK2tra6urq+vr7CwsLGxsbKysrOzs7S0tLW1tba2tre3t7i4uLm5ubq6uru7u7y8vL29vb6+vr+/v8DAwMHBwcLCwsPDw8TExMXFxcbGxsfHx8jIyMnJycrKysvLy8zMzM3Nzc7Ozs/Pz9DQ0NHR0dLS0tPT09TU1NXV1dbW1tfX19jY2NnZ2dra2tvb29zc3N3d3d7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h6QIFRuAEnCHGVpGYS8GrBiMGffAFbtAGTbCtiUxY1RIAZSCPlTECquAMXRAIuNAHiHDRSxEH/AIDMkMqVe0YSTgsALguBtECu6YtuvopRXDh1XJQVI4Q1mAHfzAECtAApPcAKCAGoU11XxFpldGvvRGnX94RYIIIwHCrXD4szzvn/VHcfG4d7vK1fy4feeI96NqB48OSn4ZeHgSVLku06OwhLyMH6eZR6JQOHdd06evh55oeHJze6bpRLlYhqqD+Heq8I3ta6uUhDC1wA6H5kKq+HoLAARKACLaQDDDwMLG+67ze677+68Ae7MI+7MRe7MZ+7Mie7Mq+7Mze7M7+7NAe7dKOKAAAIf8LUElBTllHSUYxLjAWSW1hZ2UgZnJvbSBjbGlwYm9hcmQBIAA7
--xxxsrixxx--}
This always seems to return
{
"error": {
"message": "(#324) Requires upload file",
"type": "OAuthException",
"code": 324
}
}
Few questions:
Does facebook source parameter accept base64 encoded image data?
Has anyone tried using the source parameter in the graph api explorer and have a working sample?
should the source param value should be url-encoded?
Note: My access token has all the required permissions and if i am not wrong, my encoding seems to be wrong. The base64 encoded text above is a complete image and i could decode it and get the image.
Ok. This is going to sound crazy. After a week of trying I was able to post an image successfully to the facebook wall. The fix was to send the raw binary data as it is (no base64 encoding - fb does not like it) and make sure your multipart/form-data is correct. For instance, if you boundary is defined as "boundary=--xxxsrixxx" your actual request should look like this "----xxxsrixxx\r\nContent-Disposition......\r\n----xxxsrixxx"
This works fine for me, it only uses native JavaScript features and a file input:
const fileReader = new FileReader();
const file = document.getElementById('imageInput').files[0];
fileReader.onloadend = async () => {
const photoData = new Blob([fileReader.result], {type: 'image/jpg'});
const formData = new FormData();
formData.append('access_token', pageAccessToken);
formData.append('source', photoData);
formData.append('message', 'some status message');
let response = await fetch(`https://graph.facebook.com/${pageId}/photos`, {
body: formData,
method: 'post'
});
response = await response.json();
console.log(response);
};
fileReader.readAsArrayBuffer(file);
More information: http://www.devils-heaven.com/facebook-javascript-sdk-photo-upload-with-formdata/

File upload failure on Google Drive API

I'm trying to implement REST client for Google Drive with Jersey 2.0.
According to following document, I made the code that sends files by multipart request.
https://developers.google.com/drive/v2/reference/files/insert
https://developers.google.com/drive/manage-uploads
String targetUrl = "https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart&access_token=" + token.access_token;
WebTarget target = client.target(targetUrl);
final FileDataBodyPart filePart = new FileDataBodyPart("file", file);
final MultiPart multipart = new FormDataMultiPart().field("title", file.getName())
.field("description", file.getName())
.bodyPart(filePart);
Response response = target.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(multipart, multipart.getMediaType()));
System.out.println("response:" + response.readEntity(String.class));
Here's the POST request by the code.
POST /upload/drive/v2/files?uploadType=multipart&access_token={ACCESS_TOKEN} HTTP/1.1\r\n
Accept: application/json\r\n
Content-Type: multipart/form-data; boundary=Boundary_1_1833261898_1373877178038\r\n
User-Agent: Jersey/2.0 (HttpUrlConnection 1.7.0_25)\r\n
MIME-Version: 1.0\r\n
Host: www.googleapis.com\r\n
Content-Length: 42430\r\n
MIME Multipart Media Encapsulation, Type: multipart/form-data, Boundary: "Boundary_1_1833261898_1373877178038"
Type: multipart/form-data
First boundary: --Boundary_1_1833261898_1373877178038\r\n
Encapsulated multipart part: (text/plain)
Content-Type: text/plain\r\n
Content-Disposition: form-data; name="title"\r\n\r\n
Line-based text data: text/plain
Boundary: \r\n--Boundary_1_1833261898_1373877178038\r\n
Encapsulated multipart part: (text/plain)
Content-Type: text/plain\r\n
Content-Disposition: form-data; name="description"\r\n\r\n
Line-based text data: text/plain
Boundary: \r\n--Boundary_1_1833261898_1373877178038\r\n
Encapsulated multipart part: (text/plain)
Content-Type: text/plain\r\n
Content-Disposition: form-data; filename="GoogleDrive.txt"; modification-date="Fri, 12 Jul 2013 08:15:47 GMT"; size=41918; name="file"\r\n\r\n
Line-based text data: text/plain
Last boundary: \r\n--Boundary_1_1833261898_1373877178038--\r\n
My post request was sent but following error occurred.
{
"error": {
"errors": [
{
"domain": "global",
"reason": "parseError",
"message": "Parse Error"
}
],
"code": 400,
"message": "Parse Error"
}
}
Please tell me how to avoid this error.
An upload request should have two multi-parts: metadata and file content and metadata part should be in JSON.
In your case you create a new form-encoded part for each attribute. Use the reference on docs to see the proper formatting: https://developers.google.com/drive/manage-uploads#multipart
final MultiPart multipart = new FormDataMultiPart().field("title", file.getName())
.field("description", file.getName())
.bodyPart(filePart);
Your multipart body should be valid JSON.

Resources