0
votes

I have been working on a Box App using the API v2 for the past few days and have successfully authenticated using OAuth2.

My app retrieves the access token successfully and I'm also able to access my Box account using the access token, however an upload of a file fails with a response of 299.

The html response I see from Box after posting an upload request has the following message "Sorry, we can't access that page." Your Box account may be temporarily unavailable. We're working on resolving the issue and should be back up soon."

I take it all 2xx errors mean that the request has been accepted but the Box server cannot handle it. Given below is a snippet of my code used to post the file.

Any tips on what could be wrong is appreciated

I am following instructions from http://developers.box.com/get-started/#uploading-and-downloading

    QUrl requrl = QUrl("https://www.box.com/api/2.0/files/content");

    std::string token = m_acc_token;

    QString hdrval = "Bearer "+QString(token.c_str());

    QNetworkRequest qnr(requrl);
    qnr.setRawHeader("Authorization",hdrval.toUtf8());  

    QString boundary;
    boundary = "---------7d935033608e2";

    QByteArray data;    
    data.append("[email protected]");
    data.append(boundary);
    data.append("folder_id=0");
    data.append(boundary);


    qnr.setHeader(QNetworkRequest::ContentTypeHeader,"multipart/form-data; boundary=---------7d935033608e2");
    qnr.setHeader(QNetworkRequest::ContentLengthHeader,data.size());

    QNetworkReply* areply = NULL; 
    areply = m_networkManager->post(qnr,data);
2
What language are you developing this in? Looks like java, but I'm not sure it isn't just raw C or C++Peter
I've written it in QT. Like I said I've been successful in receiving the access token as well accessing the user's content on their Box account.dingo
QT is just a framework. I use the Microsoft compilerdingo

2 Answers

1
votes

you can implement it like

QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart headerPart;
headerPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"parent_id\" \" "));
headerPart.setBody(QString(aParentFolderId).toLatin1());

QHttpPart textPartData;
textPartData.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; filename=\"filename\" \" "));
textPartData.setBodyDevice(&File); //file must be open. 
File.setParent(multiPart);
multiPart->append(headerPart);
multiPart->append(textPartData);
QNetworkRequest networkReq;
networkReq.setUrl(QUrl("https://upload.box.com/api/2.0/files/content"));
networkReq.setRawHeader("Authorization", "Bearer " + AccessToken.toLatin1());

networkReply = mNetworkAccessManager.post(networkReq, multiPart);
multiPart->setParent(networkReply);
0
votes

The curl call in the Box API documentation can't be translated directly to code as you have done. the [email protected] line on the command line puts the contents of file btest.text as the value of the parameter file.

Additionally, your multipart boundaries are malformed: they must end in \r\n; one must be present at the start of the multipart body, and another boundary with a slightly different format must be present as a final boundary. If you are interested in manually implementing the multipart form data, I'd recommend reading RFC 1876.

The Box API will return a 500 response if it is sent a malformed multipart POST body.

I'd recommend using QHttpMultiPart, for multipart form uploads, which is part of the Qt framework.