Skip to content
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

Closed
wants to merge 1 commit into from
Closed

Complex form multipart example #1901

wants to merge 1 commit into from

Conversation

PaulGWebster
Copy link

@PaulGWebster PaulGWebster commented Jan 7, 2022

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 :)

@PaulGWebster PaulGWebster changed the title Created form multipart example Complex form multipart example Jan 7, 2022
@kraih
Copy link
Member

kraih commented Jan 7, 2022

Always squash your commits.

'file2' => {
'file' => '/some/file/somewhere.xml'
}
);
Copy link
Member

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.

Copy link
Author

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

@@ -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
Copy link
Member

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? 🤨

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...accepting a JSON response.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

@@ -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/"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent quotes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

@kraih
Copy link
Member

kraih commented Jan 7, 2022

👎 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
@PaulGWebster
Copy link
Author

👎 from me i'm afraid. Not opposed to having more forma examples, but this doesn't look clean.

I updated all but one of your exceptions, the one I have not updated required your input

Copy link
Contributor

@kathackeray kathackeray left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PaulGWebster

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.

@PaulGWebster PaulGWebster closed this by deleting the head repository May 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants