0

I am trying to upload files to a RESTful WEB API using HTTP POST and cURL. Following is the code block that I am trying at this moment. With this everything just works fine other than one problem, it stores the data in the default database principal. Which I want is to upload the file to the database principal the user is currently using. I need to know two things -

  • Is there a way to specify which database principal to use in the web server in cURL header or is there any cURL option available to specify that so that cURL POST uploads the data in the mentioned database principal?

  • I have never used cURL before, is it the best option to upload files using multipart/form-data to a RESTful WEB API by HTTP POST?

    for($i=0; $i<count($_FILES['files_to_upload']['name']); $i++) {
            if(!empty($_FILES['files_to_upload']['tmp_name'][$i])) {
                $ext = pathinfo($_FILES['files_to_upload']['name'][$i], PATHINFO_EXTENSION);
                $finfo = finfo_open(FILEINFO_MIME_TYPE);
                $mime = finfo_file($finfo, $_FILES['files_to_upload']['tmp_name'][$i]);
                $data_for_api = [
                    'rel_type'  => PROJECTS_REL_TYPE,
                    'rel_key'   => $ktr,
                    'category'  => $_POST['file_category'][$i],
                    'filename'  => $_POST['file_name'][$i].".".$ext,
                    'file-form-data'    => new CURLFile($_FILES['files_to_upload']['tmp_name'][$i],$mime,$_FILES['files_to_upload']['tmp_name'][$i]),
                    'description'   => $_POST['file_name'][$i],
                    'uploader'  => $this->session->userdata(RESNR)
                ];
    
                $ch = curl_init(INTERNAL_API_URL.POST_A_FILE);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data_for_api);
                if(!curl_exec($ch)) {
                    echo curl_error($ch);
                } 
                curl_close($ch);
            }
    }
    
Shuvro
  • 1,499
  • 4
  • 14
  • 34
  • @fabrik I didn't ask how to upload a file using curl with PHP. I wanted to know something completely different. :| – Shuvro Sep 24 '18 at 13:39
  • `Using cURL for uploading files to RESTful Web API using HTTP POST` – fabrik Sep 24 '18 at 13:42
  • you should `curl_close()` no matter `curl_exec()` was successful or not. – Marcin Orlowski Sep 24 '18 at 13:43
  • @fabrik thanks. Updated the question. – Shuvro Sep 24 '18 at 13:45
  • @MarcinOrlowski yeah, you are right. Updated the question. – Shuvro Sep 24 '18 at 13:50
  • 1
    curl (well, HTTP really) has no idea what the remote server does with the data when it arrives on the server. Where the data is stored (e.g. in which database) is entirely under the server script's control. This is not some intrinsic feature of HTTP, so there is no header for it or anything. HTTP has no idea about databases and doesn't care - most HTTP requests don't involve one. If the server method you're calling chooses to accept a parameter value in POST which allows you specify an option such as this, then that's the only way you can do it. Just like any other parameter value really. – ADyson Sep 24 '18 at 13:51
  • 1
    "is it the best option to upload files using multipart/form-data to a RESTful WEB API by HTTP POST?"...it's a pretty standard way to do it, but you first have to check what format the server is expecting to receive the data in. There's always a chance it expects you to do something else. Again, there is no "this-is-always-correct" answer, it depends how the code which responds to your HTTP request has been implemented. If you didn't write that code, then check with the people who did, or consult their documentation, if they provided any. – ADyson Sep 24 '18 at 13:52
  • I reopened this question. I'm not sure if it's a good question for stack overflow, but the question that was picked for the 'Duplicate question' reason clearly was the wrong one. – Evert Sep 24 '18 at 13:55
  • So please lay off the 'close question' button @MarcinOrlowski unless you intend you spend the time to read the question. – Evert Sep 24 '18 at 13:58

1 Answers1

1

Really there's 2 questions here, which isn't a great format for stack-overflow, but I'll answer both since your experience has been pretty painful so far:

Is there a way to specify which database principal to use in the web server in cURL header or is there any cURL option available to specify that so that cURL POST uploads the data in the mentioned database principal?

The general answer to this is 'no'. CURL knows nothing about your database and only speaks HTTP. The HTTP protocol has no direct relationship with databases and it's users. It doesn't mean it's impossible, but this is defined 'per application' and on the server-side.

Your server-side API needs to provide a way to do this. Only once that API does this, CURL might be used to take advantage of that feature. But there is no 'general answer' to this.

I have never used cURL before, is it the best option to upload files using multipart/form-data to a RESTful WEB API by HTTP POST?

Generally a RESTful api has several ways to implement file uploads. If you only control the client and not the server, it doesn't matter if it's the best option. The REST api will define how uploads should work and you need to use that method.

If you control both server and client, it might be better to not use multipart/form-data, but use PUT requests instead and directly create new resources instead of wrapping them in a form/post request.

Anyway, if you are writing both the server and client and want to know more about these answers, please just close this question and open 2 new ones.

Evert
  • 93,428
  • 18
  • 118
  • 189