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

Cannot post files using Request #289

Open
codealfa opened this issue Aug 11, 2017 · 9 comments
Open

Cannot post files using Request #289

codealfa opened this issue Aug 11, 2017 · 9 comments

Comments

@codealfa
Copy link

It's impossible to post files by http using cURL via the Request API.

If you look at the code in the setup_handle method in Requests_Transport_cURL class found in the /Requests/Transport/cURL.php file, you'll see that the POST data is always converted to a string using the PHP http_build_query function. However, according to the PHP manual, the value passed to CURLOPT_POSTFIELDS must be an array if files are passed to this option.

A possible fix could be to simply add the post value to the CURLOPT_POSTFIELDS option as an array if the Content-Type header has been given as multipart/form-data. It would be converted to a query string otherwise. According to the PHP manual, the multipart/form-data Content-Type header will be set if the post value is an array.

@biglazy
Copy link

biglazy commented Aug 16, 2017

Meet this problem too, just want to replace it with the guzzle ...

soulseekah added a commit to soulseekah/Requests that referenced this issue Feb 11, 2018
soulseekah added a commit to soulseekah/Requests that referenced this issue Feb 11, 2018
Next up: test multiple files, request_multi

See WordPress#289
@soulseekah
Copy link
Contributor

Help test https://github.com/soulseekah/Requests/tree/feature-upload and provide ideas please. A fifth parameter was introduced (just like Python Requests) to ::post and ::request - $files. Simply supply a file path and magic should happen.

$response = Requests::post('https://httpbin.org/post', array(), array('foo'=>'bar'), array(), array('file1' => '/tmp/yourfile'));

@soulseekah
Copy link
Contributor

The interface has changed to:

Requests::post('https://httpbin.org/post', array(), array('foo'=>'bar', 'file'=>new Requests_File('path/to/file'))

@manuelRod
Copy link

when is this planned to be released? :)

@soulseekah
Copy link
Contributor

No idea, check out #313

@pbearne
Copy link

pbearne commented Sep 28, 2021

I have refreshed the file uploading code
and added an helper function \WpOrg\Requests\Requests::add_files_to_body() to make it easier to attach files

I am sure there will be more changes but I thought I would have a go and get this feature moving forward

@gdarko
Copy link

gdarko commented Dec 14, 2021

This isn't still released? What a shame.

@pbearne
Copy link

pbearne commented Dec 15, 2021

soon I hope

@codente
Copy link

codente commented Feb 28, 2024

What is the status of this?

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

No branches or pull requests

7 participants