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

Support for multipart/form-data #28

Open
borisaguilar opened this issue Aug 18, 2016 · 4 comments
Open

Support for multipart/form-data #28

borisaguilar opened this issue Aug 18, 2016 · 4 comments

Comments

@borisaguilar
Copy link

Currently the script replaces the data being sent with nothing :'( I need to POST a form with a File input but the current script doesn't allow it

@softius
Copy link
Owner

softius commented Aug 19, 2016

Do you have any example that can reproduce the issue?

On 19 Aug 2016 01:45, "Boris Aramis Aguilar Rodríguez" <
[email protected]> wrote:

Currently the script replaces the data being sent with nothing :'( I need
to POST a form with a File input but the current script doesn't allow it


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#28, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAjxiU4mUbpujP09KozFGKtEha4Uo7s6ks5qhOCWgaJpZM4Jn_WP
.

@piotr-cz
Copy link

I must confirm, sending data with content-type: multipart/form-data doesn't work, for two reasons:

  • The content-type header is passed over including boundary segment (which should be removed, as CURL is supposed to manage it)
  • The $_FILES payload is not appended to CURL request

@Elavarasanlee
Copy link

Hi @piotr-cz,
I tried the solution suggested by you. I am able to see the uploaded file in /tmp/ directory of our server. When I tried to send the file to the backend service, the call is not reaching the backend service. I added logs for debugging the proxy service, and I didn't get any trace after curl_exec method. All the other API calls are working except file upload.

@Elavarasanlee
Copy link

Code Snippet:

curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);   // (re-)send headers
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);     // return response
curl_setopt($ch, CURLOPT_HEADER, true);       // enabled response headers

if(array_key_exists('CONTENT_TYPE', $_SERVER) && strpos($_SERVER['CONTENT_TYPE'], 'multipart/form-data;') === 0) {
    debug("Request has Multipart Files: " . print_r($_FILES, true));
    $filesList = $_FILES;
    if(empty($request_params)) {
        $request_params = array();
    }
    foreach ($filesList as $key => $fileProps) {
        $request_params[$key] = new CurlFile($fileProps['tmp_name'], $fileProps['type'], $fileProps['name']);
    }
    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
    debug("Request Params with CurlFile(s): " . print_r($request_params, true));
}

// add data for POST, PUT or DELETE requests
if ('POST' == $request_method) {
    $post_data = is_array($request_params) ? http_build_query($request_params) : $request_params;
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS,  $post_data);
} elseif ('PUT' == $request_method || 'DELETE' == $request_method) {
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request_method);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request_params);
}

// Set multiple options for curl according to configuration
if (is_array($curl_options) && 0 <= count($curl_options)) {
    curl_setopt_array($ch, $curl_options);
}
debug("Initiating CURL $request_method call with data: " . print_r($request_params, true));

// retrieve response (headers and content)
$response = curl_exec($ch);
curl_close($ch);

debug("Response Received from CURL: " . print_r($response, true));

In the above code I am able to see the log trace till:
debug("Initiating CURL $request_method call with data: " . print_r($request_params, true));

I need little help to proceed further.

Thank you,
Elavarasan M (Lee)

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

4 participants