0

I'm trying to use RESTSharp to create a simple folder on Box, but I'm having a hard time. I keep getting this error:

{"type":"error","status":400,"code":"bad_request","help_url":"http://developers.box.com/docs/#errors","message":"Could not parse JSON","request_id":"1474540366505ba7a11bdcd"}

This is my code:

static string box(string resourceURL, string APIKey, string authToken)
        {
            RestClient client = new RestClient();
            client.BaseUrl = "https://api.box.com/2.0";
            var request = new RestRequest(Method.POST);
            request.Resource = resourceURL;
            string Headers = string.Format("Authorization: BoxAuth api_key={0}&auth_token={1}", APIKey, authToken);
            request.AddHeader("Authorization", Headers);
            request.AddParameter("name", "TestFolder");

            // request.RequestFormat = DataFormat.Json;
            var response = client.Execute(request);
            return response.Content;
        }

What am I missing? Thanks in advance for your help.

2
  • If you look at developers.box.com/docs on the right, /folders is the path that you need to post a JSON folder object in the body. Commented Sep 21, 2012 at 1:16
  • It's not a querystring parameter Commented Sep 21, 2012 at 1:16

4 Answers 4

1

You may also want to take a look at a recently created github repo, where some folks are collaborating on a C# Box SDK. https://github.com/jhoerr/box-csharp-sdk-v2

Sign up to request clarification or add additional context in comments.

Comments

0

I see two issues:

  • The URL needs to point to /folder/{folder_id} (0 is the id of the root folder)
  • The folder name needs to be in the json body of the request, not a query parameter

I'm not that familiar with C# or RESTSharp, but I believe this code should address the two problems.

static string box(string APIKey, string authToken)
            {
                RestClient client = new RestClient();
                client.BaseUrl = "https://api.box.com/2.0";
                var request = new RestRequest(Method.POST);
                request.Resource = "/folders/0";
                string Headers = string.Format("BoxAuth api_key={0}&auth_token={1}", APIKey, authToken);
                request.AddHeader("Authorization", Headers);
                request.AddParameter("text/json", "{\"name\" : \"TestFolderName\"}", ParameterType.RequestBody);

                //request.RequestFormat = DataFormat.Json;
                var response = client.Execute(request);
                return response.Content;
            }

Comments

0

Thanks for your help, this is the exact code that finally worked.

static string box(string APIKey, string authToken)
            {
                RestClient client = new RestClient();
                client.BaseUrl = "https://api.box.com/2.0";
                var request = new RestRequest(Method.POST);
                request.Resource = "/folders/0";
                string Headers = string.Format("BoxAuth api_key={0}&auth_token={1}", APIKey, authToken);
                request.AddHeader("Authorization", Headers);
                request.AddParameter("text/json", "{\"name\" : \"TestFolderName\"}", ParameterType.RequestBody);

                //request.RequestFormat = DataFormat.Json;
                var response = client.Execute(request);
                return response.Content;
            }

Comments

0

static string folderCreation(string APIKey, string authToken) {

    RestClient client = new RestClient();
    client.BaseUrl = "https://api.box.com/2.0/folders";
    var request = new RestRequest(Method.POST);
    string Headers = string.Format("Bearer {0}", authToken);
    request.AddHeader("Authorization", Headers);
    request.AddParameter("application/json", "{\"name\":\"Youka\",\"parent\":{\"id\":\"0\"}}", ParameterType.RequestBody);
    var response = client.Execute(request);
    return response.Content;



}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.