Welcome Guest, Not a member yet? Register   Sign In
CURLRequest multipart File - nothing send
#1

(This post was last modified: 02-01-2020, 08:17 AM by Tysonpower.)

Hello everybody,

i currently working on a function in a Project of mine where i need to send a file over POST to an API.

I used CURLRequest for a POST and GET Api before what works great, but i can't get the multipart File Upload working.
I tested it with webhook.site to see what is send. The header is set to multipart, so the multipart option seems to work like it is described in the docs. But the file size is 0 byte and the form parameters are also missing.

Did i just miss something or is there a bug? Here is my Codesnippet, the File is created and is not empty, the path variable also has the right path in it. HTTP Response is 200 OK.

Code:
$client = \Config\Services::curlrequest();

// create temp file to upload
$tmpfname = tempnam(sys_get_temp_dir(), 'temp_file');

$handle = fopen($tmpfname, "w");
fwrite($handle, 'test data u know?');
fclose($handle);

// fill post data
$post_data = [
   'para' => 'testparam',
   'file' => new \CURLFile($tmpfname)
   ];

$response = $client->request('POST', 'https://webhook.site/randomiduknow', [
   'multipart' => $post_data
   ]);

Hope someone has an idea so that i don't have to use curl on it's own Smile

regards,
Manuel
Reply
#2

I have made a couple of tests myself. I can't send any file with or without* CodeIgniter cURL class to webhook.site. Haven't found any resources that you should be able to send files to them or not.

Using PHP 7.3.12 I can't send anything with CodeIgniter, the same as you. But with PHP 7.4 it takes my "form values".
If I only append a string (not an CURLFile) CodeIgniter won't work with PHP 7.4 either. It will always be content-length: 0.

I can however send "form values" with cURL stand alone with both 7.3.12 and 7.4.

There aren't any solution in my post. Just wanted to say you aren't alone in this madness...

* Using curl stand alone.
Reply
#3

(02-01-2020, 09:31 AM)jreklund Wrote: I have made a couple of tests myself. I can't send any file with or without* CodeIgniter cURL class to webhook.site. Haven't found any resources that you should be able to send files to them or not.

Using PHP 7.3.12 I can't send anything with CodeIgniter, the same as you. But with PHP 7.4 it takes my "form values".
If I only append a string (not an CURLFile) CodeIgniter won't work with PHP 7.4 either. It will always be content-length: 0.

I can however send "form values" with cURL stand alone with both 7.3.12 and 7.4.

There aren't any solution in my post. Just wanted to say you aren't alone in this madness...

* Using curl stand alone.

To use webhook you need to add a personal key for the url.

Here are two requests, one with a form and one with the coding in my post: https://webhook.site/#!/d24f29a8-2146-45...7889dbe9/1

Using form_params without a file instead of multipart works btw. with the codeigniter 4 CURL.
Reply
#4

I did add an key for my URL and I where able to see everything I posted. form_params works flawlessly, but as the post where about sending a file and multipart, my answer reflects my findings with multipart.

And the only time I could get webhook.site to register content with multipart where when I submitted this array. If I only posted 'text' without any file, the content-length resulted to 0. And thus YMCA never where displayed (submitted?). And it only worked with PHP 7.4.
PHP Code:
$post_data = [
    
'text' => 'YMCA',
    
'file' => new \CURLFile(FCPATH.'robots.txt''text/plain''robots.txt')
]; 
Reply
#5

(02-01-2020, 10:44 AM)jreklund Wrote: I did add an key for my URL and I where able to see everything I posted. form_params works flawlessly, but as the post where about sending a file and multipart, my answer reflects my findings with multipart.

And the only time I could get webhook.site to register content with multipart where when I submitted this array. If I only posted 'text' without any file, the content-length resulted to 0. And thus YMCA never where displayed (submitted?). And it only worked with PHP 7.4.
PHP Code:
$post_data = [
    'text' => 'YMCA',
    'file' => new \CURLFile(FCPATH.'robots.txt''text/plain''robots.txt')
]; 

okay, so it seems to be a Problem with the PHP Version? I only have 7.3.10 on my server sadly. Any Idea how to fix it or an alternative for a post multipart request?

PS: tested the above on my server with php 7.3 and you are right, just doesen't work Sad
Reply
#6

No, the PHP 7.4 thing are with Codeigniter. I got it to work in PHP 7.3.12 with just calling cURL myself. But with no method I where able to add an file. So I don't know if webhook.site can take attachments or I'm sending it incorrectly.
Reply
#7

(02-01-2020, 11:49 AM)jreklund Wrote: No, the PHP 7.4 thing are with Codeigniter. I got it to work in PHP 7.3.12 with just calling cURL myself. But with no method I where able to add an file. So I don't know if webhook.site can take attachments or I'm sending it incorrectly.

Okay, if you use just a normal html form it works fine on the webhook testsite. see here: https://webhook.site/#!/d24f29a8-2146-45cd-a67a-214109b33b25/5f034f01-a4c7-455d-9440-eab2f70f3f51/1

W
ill be using curl without CI4 for now, thanks for the help. Happy i din't make some mistake in my coding, but it would be easier to fix Smile
Reply
#8

Hi, i'm trying to send a file over post API with multipart but after follow all the documentation and many post (this one included), i have the same issue. The requests doesn't contains any file.

Here are some of tryies i did.
Code:
$res = $this->client->request('POST', $url, [
            'headers' => [
                'Authorization' => 'Bearer ' . $_SESSION['token'],
            ],
            'multipart' => [
                'Content-Type' => 'multipart/form-data',
                'userfile'        => new CURLFile($realpath)
                //'contents' => curl_file_create($realpath, $type, $filename),
            ],
            'debug' => './curl_log.txt'
        ]);
Code:
$res = $this->client->request('POST', $url, [
            'headers' => [
                'Authorization' => 'Bearer ' . $_SESSION['token'],
            ],
            'multipart' => [
                'Content-Type' => 'multipart/form-data',
                'filename'     => $filename,
                'name'         => 'image',
                'type'         => $type,
                'contents'         => new CURLFile(FCPATH.$realpath, $type, $filename)
                //'contents' => curl_file_create($realpath, $type, $filename),
            ],
            'debug' => './curl_log.txt'
        ]);

Someone could help me? Confused
Reply
#9

Hi,

This is the way I was able to post a file to an API. I hope this helps.

PHP Code:
$curl = \Config\Services::curlrequest();
        $cfile = new \CURLFile($file['full_path'], $file['file_type'], $file['file_name']);
        $data $curl->request("POST"$targetURL, [
                            'auth' => [$user$password],
                            'headers' => [
                                'Content-type' => 'multipart/form-data'
                            ],
                            'multipart' => [
                                "operation" => $operation,
                                "id1" => $abc,
                                "id2" => $cde,
                                "filedata" => $cfile
                            
]
                        ]); 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB