How to upload files to box? I have curl code

3.1k views Asked by At

I am integrating box cloud in my application. I am working with Qt platform.

In box documentation, they said to post like this which is in curl.

curl https://upload.box.com/api/2.0/files/content \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-F filename=@FILE_NAME \
-F folder_id=FOLDER_ID 

where to specify the filename and folder_id parameters. I have tried by posting in the body but it is giving error. And also please tell me where to post the file contents.

Thanks.

4

There are 4 answers

0
raju On BEST ANSWER

I tried by posting with multipart/form-data using the Qt API. I am not familiar with using libcurl in Qt. But I got success with Qt API only. Here is my code.

multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

        QByteArray boundary("56354654654654321768987465413574634354658");
        multiPart->setBoundary(boundary);

        QHttpPart folderPart;
        folderPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"folder_id\""));
        folderPart.setBody(folderId.toUtf8());

        QString fileExt = fileToUpload.split(".").takeLast();
        QString mimeType = FileUtilities::getMIMEType("." + fileExt);

        QString fileName = fileToUpload.split("/").takeLast();
        qDebug() << "file name to upload : " << fileName;

        QHttpPart textPart;
        textPart.setHeader(QNetworkRequest::ContentTypeHeader, mimeType);
        textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; filename=\""+ fileName +"\"; name=\"filename\";"));

        QFile *fileUpload = new QFile(fileToUpload);
        fileUpload->open(QIODevice::ReadOnly);

        textPart.setBodyDevice(fileUpload);

        multiPart->append(textPart);
        multiPart->append(folderPart);
req.setUrl(QUrl("https://upload.box.com/api/2.0/files/content"));
QByteArray ba_auth = QString("Bearer " + m_accessToken).toUtf8();
req.setRawHeader("Authorization", ba_auth);


m_netManager->post(req, multiPart);

And the reply will be the details of the file that has been uploaded to box.com. Thanks everybody for helping me in this topic.

  • Raju
1
DeeDee On

EDIT: It looks like you removed your PHP tag right after I posted my answer. This may not be relevant to your question then.

It appears that the box documentation is telling you how to execute a curl transfer from the linux command line. We just need to translate this to PHP.

In your PHP, you first need to get a path to your file in the appropriate fashion:

$filePath = realpath('./file_name');

Store your access token in a variable for use later:

$accessToken = "YOUR_ACCESS_TOKEN";

Then you need to set up a curl handle:

$ch = curl_init();

Put your POST data into an array. This is equivalent to what's in those -F arguments:

$post = array('filename'=>'@'.$filePath, 'folder_id'=>'my_fave_folder');

Then you need to set specific curl options:

curl_setopt($ch, CURLOPT_URL, "https://upload.box.com/api/2.0/files/content");
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $accessToken"));

Then you execute the curl and capture the response in $result, which you can echo and examine:

$result=curl_exec($ch);
curl_close($ch);
echo $result;

You may have to set some SSL options too since you're accessing via https.

0
Lucaci Andrei On

EDIT: if using PHP. For QT check other StackOverflow question on how to send libcurl requests via C++

You can also use this header for general purposes:

$defaultOptions = array(
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_VERBOSE        => true,
    CURLOPT_HEADER         => false,
    CURLINFO_HEADER_OUT    => false,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => false,
);

and you can set it via the curl_setopt_array() function:

curl_setopt_array($curlhandle, $defaultOptions);
2
Александр Степаненко On

Additionally I want to notice that the way shown in official documentation is not working. I tried to implement solution from https://developer.box.com/reference#upload-a-file that looks like this:

curl https://upload.box.com/api/2.0/files/content \ 
  -H "Authorization: Bearer ACCESS_TOKEN" -X POST \
  -F attributes='{"name":"tigers.jpeg", "parent":{"id":"11446498"}}' \
  -F [email protected]

But I've got "bad request" error from server all the time. But solution shown by Raju is still working. May be somebody knows how to implement it in QT.