-
Notifications
You must be signed in to change notification settings - Fork 576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Complex form multipart example #1901
Conversation
Always squash your commits. |
'file2' => { | ||
'file' => '/some/file/somewhere.xml' | ||
} | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That does not look like our standard formatting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would the standard formatting look like so I may update it
lib/Mojo/UserAgent.pm
Outdated
@@ -392,6 +392,24 @@ Mojo::UserAgent - Non-blocking I/O HTTP and WebSocket user agent | |||
# Form POST (application/x-www-form-urlencoded) | |||
my $tx = $ua->post('https://metacpan.org/search' => form => {q => 'mojo'}); | |||
|
|||
# Form multipart post with both a local file and document within a string, accepting a json return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lower case post
and json
? 🤨
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...accepting a JSON response
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated
lib/Mojo/UserAgent.pm
Outdated
@@ -392,6 +392,24 @@ Mojo::UserAgent - Non-blocking I/O HTTP and WebSocket user agent | |||
# Form POST (application/x-www-form-urlencoded) | |||
my $tx = $ua->post('https://metacpan.org/search' => form => {q => 'mojo'}); | |||
|
|||
# Form multipart post with both a local file and document within a string, accepting a json return | |||
my $tx = $ua->post( | |||
"https://example.com/" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent quotes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated
👎 from me i'm afraid. Not opposed to having more form examples, but this doesn't look clean. |
Updated scalar to string in text label Cleaned up text capitalization and quotes
I updated all but one of your exceptions, the one I have not updated required your input |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your example code has improperly nested brackets (so throws an error when run). I've slightly reformatted and commented your example to demonstrate:
my $tx = $ua->post(
'https://example.com/' => {'Accept' => 'application/json'} => form =>
{
'file1' => {
'content' => '<?xml version="1.0"?><P ID="greeting">Hi!</P>',
'type' => 'text/xml',
'filename' => 'submit.xml'
}
} =>
{
'file2' => { # <-------------------------------------- whoops!!
'file' => '/some/file/somewhere.xml'
}
);
As for the formatting, I've compared it to the other examples in the synopsis and the docs for Mojo::UserAgent::Transactor, and I've had a go at reformatting your example below to see if it is more in keeping with expectations. Note that I have removed the Accept
header, as this seems to distract from the purpose of the example. Also I have combined both parts into a single hashref.
# Form multipart POST with both a local file upload and document within a string
my $tx = $ua->post('http://localhost:3000/' => form => {
file_upload => {
file => 'test-file.txt',
},
file_string => {
content => '<?xml version="1.0"?><p id="greeting">How do!</p>',
type => 'text/xml',
filename => 'submit.xml'
}
}
);
This doesn't have the project's perltidy rules applied, but I've noticed that other examples in the synopsis appear to have prioritised clarity over adherence to those rules. If perltidy conformance is required for the example, it would look like this:
# Form multipart POST with both a local file upload and document within a string
my $tx = $ua->post(
'http://localhost:3000/' => form => {
file_upload => {file => 'test-file.txt',},
file_string =>
{content => '<?xml version="1.0"?><p id="greeting">How do!</p>', type => 'text/xml', filename => 'submit.xml'}
}
);
I tested it against a local netcat
server:
$ nc -kdl localhost 3000
POST / HTTP/1.1
Content-Type: multipart/form-data; boundary=FAnFX
Accept-Encoding: gzip
User-Agent: Mojolicious (Perl)
Host: localhost:3000
Content-Length: 268
--FAnFX
type: text/xml
Content-Disposition: form-data; name="file_string"; filename="submit.xml"
<?xml version="1.0"?><p id="greeting">How do!</p>
--FAnFX
Content-Disposition: form-data; name="file_upload"; filename="test-file.txt"
Hello there!
--FAnFX--
I hope this helps.
Summary
A minor addition to the examples/synopsis
Motivation
I found it really difficult to get multipart working
References
No references
Problems
There is no way to make a working example of this that will just run, unless anyone knows a site anywhere that would accept such a payload, By the way I actually expect this pull to be denied, but I want to raise that a form multipart example should be included!
Request
Also I quite literally guessed how to do the 'file1' that is that it would be 'filename' for what to name the file and what not, I could not find the documentation for it, so if anyone knows where the documentation for that section is, please let me know :)