Formatting a POST request to create a new Dropbox folder.

Nov 8, 2013 at 9:21 PM
I have been following the guidance here: http://msdn.microsoft.com/en-us/magazine/dn342869.aspx, to add Dropbox functionality into our software.

However, I'm stuck trying to create a new folder - I keep getting a "Bad Request" response.

The Dropbox documentation says that I need a POST request with two parameters (root and path).

I'm using these parameters when creating the signature:
Authentication::HTTPParameters postParameters;
postParameters[L"root"] = L"dropbox";
postParameters[L"path"] = strFolderName;

auto signatureParams = 
    oAuthObj->CreateOAuthSignedParameters(url.to_string(),
    L"POST",
    &postParameters,
    consumerKey,
    consumerSecret,
    creds->Token(),
    creds->TokenSecret()
    );
and I'm setting them to the body of my http_request. I've tried using the same format as you would in a query string. I've tried using a json object. I've tried creating a json object and then converting it to a string.

Can anyone help me figure out the exact formatting that I'm supposed to use?
Coordinator
Nov 18, 2013 at 11:22 PM
To create an empty folder, without any files, I would suggest using the "/fileops/create_folder" URL

The error code "Bad Request" in the response implies that the one of the input parameters sent is incorrect.
As per the dropbox documentation, the root and path parameter-value pairs are to be passed as query parameters in the HTTP URL, not in the request body.

Did you look at the URL that is being passed to C++Rest http_client, i.e what is the value of "sb" here: http_client client(sb);?
If it is something like this: "https://api.dropbox.com/1/fileops/create_folder?oauth_consumer_key=key1&oauth_nonce=nounce1&oauth_timestamp=1384815906&oauth_version=1.0&oauth_signature_method=PLAINTEXT&oauth_signature=signature1&oauth_token=token1",
you can clearly notice that the root and path query params are not present here.

Next step would be to add these query parameters to the URL being constructed. You can use the uri_builder::append_query() API to add these parameters to the URI.
The below code will create a folder named "newfolder" under the "dropbox" root:
    uri url(L"https://api.dropbox.com/1/fileops/create_folder");
    std::shared_ptr<OAuth> oAuthObj = std::make_shared<OAuth>();    
    auto signatureParams = 
        oAuthObj->CreateOAuthSignedParameters(url.to_string(),
        L"POST",
        NULL,
        consumerKey,
        consumerSecret,
        creds->Token(),
        creds->TokenSecret()
        );  

    std::wstring sb = oAuthObj->OAuthBuildSignedHeaders(url);  // NOTE: sb does not contain the root and path parameter values YET!!!! 

    uri_builder ub(sb);
    ub.append_query(L"root", L"dropbox");  
    ub.append_query(L"path", L"newfolder");
    sb = ub.to_string();

    http_request req;           
    http_client client(sb);     
    req.set_method(methods::POST);          
    return client.request(req)
        .then([](pplx::task<http_response> previousTask)
    { // Handle the response, look at the status code etc.
    }
The code sample provided at "Bringing RESTful Services to C++ Developers" is an illustration on how one can interact with dropbox REST service using the C++ REST SDK. It may not cover all the cases, hence I recommend understanding and debugging the sample to ensure that the correct values are being set and passed to the http_client.

Thanks
Kavya.