-
Notifications
You must be signed in to change notification settings - Fork 499
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
File uploads #313
base: develop
Are you sure you want to change the base?
File uploads #313
Conversation
Inspired by WordPress#289
Next up: test multiple files, request_multi See WordPress#289
library/Requests.php
Outdated
* @return Requests_Response | ||
*/ | ||
/** | ||
* Send a POST request | ||
*/ | ||
public static function post($url, $headers = array(), $data = array(), $options = array()) { | ||
return self::request($url, $headers, $data, self::POST, $options); | ||
public static function post($url, $headers = array(), $data = array(), $options = array(), $files = array()) { |
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.
IMO, this should either be part of $data
or part of $options
. For the former, I wouldn't be opposed to having a Requests_File
interface (with a default implementation) so that you could do e.g:
$response = Requests::post(
'http://example.com',
[ 'Content-Type' => 'text/plain' ],
new Requests_File( __DIR__ . '/README.txt' )
);
(And potentially allow an array of these.)
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's a good idea, would allow us to set filenames and mime-types, just like cURL_File does, I was originally intending to do it as part of data, then remembered how this library is based on the Python Requests library and thought that following their convention would be appreciated.
If not, I have nothing against wrapping into a Requests_File
instead.
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.
Python has the benefit of named parameters, so in Python it's:
requests.post( 'http://example.com', files={ 'file': open( 'x', 'rb' ) } )
The equivalent for PHP is our $options
array, but we already deviate a bit anyway due to the nature of the language, so I think overloading the $data
parameter makes more sense.
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.
True, makes sense. I think Requests_File
is the more elegant way of doing it then. I'll work on this a bit and send more stuff into here. Thanks.
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.
btw, for the interface, I'd recommend calling it Requests_File_Interface
, and having get_headers()
and get_body()
methods, which should allow an advanced implementation to offer multipart uploads (although maybe not in Requests itself) or uploading using a data string.
The default implementation should be Requests_File
, and we should be able to imply most stuff (upload name, size, etc) from the filename passed in.
Codecov Report
@@ Coverage Diff @@
## master #313 +/- ##
============================================
- Coverage 92.11% 92.04% -0.07%
- Complexity 760 771 +11
============================================
Files 21 21
Lines 1762 1785 +23
============================================
+ Hits 1623 1643 +20
- Misses 139 142 +3
Continue to review full report at Codecov.
|
Coded things up around a
Coding multichunk uploading to save RAM is possible for fsockopen, but it's really just fsockopen low-level internals and none of the other transports will use this as cURL handles large uploads intelligently on its own. Handling large uploads using fsockopen efficiently should be outside of the scope of this PR in order to get the feature in with less blood and tears. Let me know what you think. |
These definitely need to be part of an interface to allow users of the library to create alternative implementations. For example, it should support the following use cases:
(For any case other than multiple files, we probably shouldn't use Having an interface expose |
cURL doesn't allow streaming for multipart/form-data, using cURLFile is the only way to upload a file as part of the data. And CURLFile only accepts a path. Thus:
On multipart/form-data you'd have to save the contents to a temporary file first for cURL.
Not possible to get a filepath from a file descriptor, especially since it could be a
This would only be possible in cURL by using
This would grow the code by a whole branch in fsockopen. cURL sends one file with multipart/form-data (even from the command line), unless, you want to employ
The only common things among the transports at this point and the foreseeable future are: 1. filepath, 2. filename, 3. mimetype. Even
Pointless unless you want to use them inside To summarize:
I feel this over-engineering is hindering a straightforward feature from landing and doing what it's supposed to do - upload a file. Decisions not options. |
Any info about this feature? It would be a great addition. I had an issue where the call towards an API passed when I used |
Working on #289 :)