Parse multipart/form-data cakephp 3 - rest

I'm currently trying to parse the multipart/form-data that I send through Postman plugin in Chrome. However, I get the following output:
'------WebKitFormBoundarymsXhoqlRBbTbsZFb
Content-Disposition: form-data; name="album_id"
2
------WebKitFormBoundarymsXhoqlRBbTbsZFb
Content-Disposition: form-data; name="description"
haiahaahahahdaisdhisadhisadihsdhiiahsd
------WebKitFormBoundarymsXhoqlRBbTbsZFb
Content-Disposition: form-data; name="favorite"
true
------WebKitFormBoundarymsXhoqlRBbTbsZFb
Content-Disposition: form-data; name="uploadfile"; filename="test2.txt"
Content-Type: text/plain
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
------WebKitFormBoundarymsXhoqlRBbTbsZFb
Content-Disposition: form-data; name="uploadfile"; filename="test.txt"
Content-Type: text/plain
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
------WebKitFormBoundarymsXhoqlRBbTbsZFb--
'
This output do I get when I try to debug $this->request->input(). When I try $this->request->input('json_decode') I get an empty array, so I'm assuming that the data isn't in a proper format (just a string). Before I write my own algorithm I want to be sure that I'm not reinventing the wheel with this one. I'm doing something wrong? Or if not, does there exist some Cakephp 3 function which takes care of this?
Update
I found what is wrong in my code and it seems like the routing process somehow empties the post array to early. When I try Postman with the following endpoint http://vecto.app/api/pictures $this->request->data is empty. However, when I try to access the endpoint http://vecto.app/pictures the $this->request->data is filled with the information. Does anyone know what is wrong with the following routing setup:
Router::prefix('api', function ($routes) {
$routes->extensions(['json', 'xml']);
$routes->resources('Users');
$routes->resources('Pictures');
// We connect the /register action so we can simply extend the CRUD Plugin add() method
// and benefit of already available logic like validation and response codes instead of having to reinvent the wheel.
Router::connect('/api/users/register', ['controller' => 'Users', 'action' => 'add', 'prefix' => 'api']);
$routes->fallbacks('InflectedRoute');
});

I finally found the solution which was a beforeFilter that emptied the $this->request->data array. Therefore, the input array wasn't empty but the data array was empty. This is the code that caused the problem:
public function beforeFilter(Event $event) {
if ($this->request->is('post')) {
$data = $this->request;
$this->request->data = $this->request->input(function ($data) {
return json_decode($data, true);
});
}
}

Related

vim diff view for long lines

Is there a solution for getting a useful vim diff with very long lines? The problem I have is that for example I have one long line in one file:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
In another file I have a line very similar, but I have two changes. I capitalize ipsum to IPSUM and Duis to DUIS.
Lorem IPSUM dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. DUIS aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
The problem is that now, when I compare these two files with :diffthis, vimdiff highlights everything between IPSUM and DUIS, and this is really useless to look at. I would like to just see highlighted LOREM and DUIS and their lowercase counterparts. Here is an image of what my vimdiff view looks like. Is there a way to achieve this?
The behaviour I want is actually the default diff view in pycharm. This image is from pycharm:
I found a plugin that does what I am asking for:
https://github.com/rickhowe/diffchar.vim

Display full string in F# interactive window

If I type the following into an fsx in Visual Studio Code
let longString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
and run F# interactive on it by highlighting and pressing Alt+Enter, the output in the interactive window is
- let longString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepte- ur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";;
val longString : string =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed "+[384 chars]
or as screenshot:
How can I tell the F# interactive window to display the full string? Is there a shortcut or else?
I am aware of this post, but as fas as I know this is different here because a string is not lazily-evaluated.
printfn will print it completely:
printfn "%s" longString
Alternatively, you can change the fsi object properties, for instance:
fsi.PrintWidth <- 1000;;
Also, you can use AddPrinter:
fsi.AddPrinter (fun (s:string) -> s)
Here is the documentation: https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/fsharp-interactive-options#f-interactive-structured-printing
If it is just for testing/debugging:
If you are in VSC you can run your script interactively as you mentioned. You can take advantage of that by using the interactive terminal and typing (in said interactive terminal)
longString;;
//output
val it : string =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
This will then show the whole string and is in most cases sufficient (since you do not want to clutter your script with unnecessary outputs anyway).

In flutter development, how to print long string in visual studio code debug console?

I want to print large string in flutter but they are truncated.
Example, this is 1342 characters string:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum
dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur
adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad'); // minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.
Only the top 1022 characters will be printed.
I try to use debugPrint and print functions but nothing.
What I can do?
That's due to VS Code truncating big strings, usually bigger than 1000 char of being printed at once.
You can try a little "hack" by splitting it into words and print each of it.
myLongString.split(' ').forEach((word) => print(" " + word));
You can also try to use the logging package, but probably it will experience the same problem when trying to print the whole string, since it is trying to stdout all at once.
you can use log(message) function.

How can I stop MailApp.sendEmail() from adding line breaks to an email body every ~80 chars?

I am using a google script attached to a google sheet in my Google Drive to send emails. I have noticed that MailApp.sendEmail() sends emails with a body that have doesn't quite look like the original: it adds a line break every ~75 characters (without interrupting words).
How can I stop the MailApp.sendEmail() function from doing this?
Here's an example to illustrate:
Running this function :
function sendTestEmail(){
var text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
MailApp.sendEmail("test#gmail.com", "test", text);
}
Will give an email that looks like this
In case it's useful, I tested a bit: a 76 character sentence stays on one line (no more), but adding an extra word made the last two words of this new sentence go to a new line.
Thanks a lot in advance !
EDIT 1: These line breaks do not appear in mac mail or thunderbird, but do appear on the iphone gmail app.
Also I tried using html instead of plain text, and it does remove the unwanted line breaks: great! But it also removes all line breaks unless I put <br> manually.
I might need to fine tune details, but as suggested the following works for me. The text.replace is needed to make the line breaks I might put in intentionally to appear in the html email.
function sendTestEmail(){
var text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
var htmlText = text.replace(/\n/g,'\n<br>');
MailApp.sendEmail({
to: "test#gmail.com",
subject: "test",
htmlBody: htmlText,
});

Asp classic mail function sends string with blank spaces

I am sending a mail with CDO.Message object and the mail arrives with a blank space between characters.
For example, I sent this string:
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
And arrives this:
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in volup tate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
Check the strong word for understand. Why occurre that?
My code
Set Mail= Server.CreateObject("CDO.Message")
Set MailConfig = Server.CreateObject("CDO.Configuration")
Mail.From = example#hotmail.com
Mail.Bcc = "example#hotmail.com"
Mail.Subject = "Example"
MailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = sendUsing
MailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtpServer
MailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = smtpServerPort
MailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
MailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = smtpUseSSL
MailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = smtpConnectionTimeout
MailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = mailUsername
MailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = mailPassword
MailConfig.Fields.Update
Mail.Configuration = MailConfig
strBody = "the string"
Mail.HTMLBody = strBody
Mail.HTMLBodyPart.Charset = "utf-8"
Mail.send
The extra space appears every 991 characters.
I added this:
Mail.HTMLBodyPart.ContentTransferEncoding = "quoted-printable"
And has been solved.
If you don't send HTML in the email body, do not make use of Mail.HTMLBody, instead use Mail.TextBody which is the correct choice when sending pure text. Textbody preserves line breaks and other control characters which HTMLBody does not.
I have experienced similar issues in Outlook when displaying raw text sent with HTMLBody, so I believe this simple change will fix it.